2015-05-25 22 views
0

我正在使用Apache Commons Configuration來讀取xml文件中的配置。 例如:配置是如何使用apache配置獲取屬性值和標記值libraray

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

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

123-> example task1 
456-> example task2 

如何可以做到這一點的值。

+0

你有閱讀和遵守的文件? https://commons.apache.org/proper/commons-configuration/userguide/howto_xml.html – nhahtdh

回答

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)?