【问题标题】:How to get Attribute value and tag value using apache configuration libraray如何使用apache配置库获取属性值和标签值
【发布时间】:2015-08-06 12:29:30
【问题描述】:

我正在使用 Apache Commons Configuration 来读取 xml 文件中的配置。 例如:配置是

<example>
    ....
    <task id="123">example task1</task>
    <task id="456">example task2</task>
    ....
</example>

我想提取下面表格中需要值的所有 example.task

123-> example task1
456-> example task2

如何实现。

【问题讨论】:

标签: java regex apache


【解决方案1】:

您可以使用 xpath 来提取您需要的信息。

XMLConfiguration config = new XMLConfiguration("ConfigTest.xml");
ConfigurationNode node = config.getRootNode();
config.getString("example/task[id= '123']"); // This returns the exact value

你也可以填充地图

Map<String, String> configMap = new HashMap<String, String>();
for (ConfigurationNode c : node.getChildren("task"))
{
    String key = (String)c.getAttribute(0).getValue();
    String value = (String)c.getValue();
    configMap.put(key, value);
}

代码专家来自: How to load xml file using apache commons configuration (java)?

【讨论】:

    猜你喜欢
    • 2013-05-09
    • 1970-01-01
    • 1970-01-01
    • 2017-11-16
    • 1970-01-01
    • 2016-07-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多