1
我使用apache commons配置1.10来管理xml配置。这是xml格式的配置文件。如何使用apache commons解析xml中的配置文件?
<?xml version="1.0" encoding="ISO-8859-1" ?>
<config>
<mainServerHostname>MainServer</mainServerHostname>
<failoverServers>
<server>
<ipAddress>192.168.0.5</ipAddress>
<priority>1</priority>
</server>
<server>
<ipAddress>192.168.0.6</ipAddress>
<priority>2</priority>
</server>
</failoverServers>
</config>
我需要打印以下
mainServerHostname=MainServer
failoverServers.server.ipAddress=192.168.0.5
failoverServers.server.priority=1
failoverServers.server.ipAddress=192.168.0.6
failoverServers.server.ipAddress=2
这是代码片段我写
public void loadXmlFile(String filename) {
XMLConfiguration config = null;
try {
config = new XMLConfiguration(filename);
} catch (ConfigurationException e) {
e.printStackTrace();
}
List<HierarchicalConfiguration> fields= config. configurationsAt("failoverServers");
for (Iterator it = fields.iterator(); it.hasNext();) {
HierarchicalConfiguration sub = (HierarchicalConfiguration) it
.next();
Iterator<String> keyIter = sub.getKeys();
String key, value;
while (keyIter.hasNext()) {
key = keyIter.next();
value = sub.getString(key);
System.out.println("Key:: " + key + " Val:: " + value);
}
}
}
这是运行在上面的代码之后的结果。因为,我无法遍历整个xml树结构。
Key:: server.ipAddress Val:: 192.168.0.5
Key:: server.priority Val:: 1
任何人都可以请帮助我如何获得所需的输出?
您首先使用哪个Apache Commons库? – 2015-02-11 14:12:42