使用 Apache Commons Configuration 2.5 从 xml 文件读取地图的最优雅方法是什么?
What is the most elegant way to read map from xml file using Apache Commons Configuration 2.5?
我遇到的问题与 this question 中的问题几乎相同。假设我有一个类似的 xml 文件,我想阅读第一张地图:
<?xml version="1.0" encoding="UTF-8"?>
<root>
<map>
<entry key="favoriteSeason">summer</entry>
<entry key="favoriteFruit">pomegranate</entry>
<entry key="favoriteDay">today</entry>
</map>
<anotherMap>
<entry key="favoriteSeason">winter</entry>
<entry key="favoriteFruit">orange</entry>
<entry key="favoriteDay">wednesday</entry>
<entry key="favoriteColor">green</entry>
</anotherMap>
</root>
注意:键值对的数量可能会有所不同。
使用 ConfigurationNode 的链接问题中的答案很好,但在 2.5 版本中 class 不存在。在 Apache's user's guide 之后,我想到了这样的事情:
Configurations configurations = new Configurations();
XMLConfiguration xmlConfiguration = configurations.xml("config.xml");
Map<String, String> map = new HashMap<>();
int pairsCount = xmlConfiguration.getList(String.class, "map.entry").size();
for(int i = 0; i < pairsCount; ++i)
map.put(xmlConfiguration.getString("map.entry(" + i + ")[@key]"), xmlConfiguration.getString("map.entry(" + i + ")"));
我觉得获取键值对的数量并不是最好的,而且将键值对放在地图中也不是那么可读。有更好的方法吗?
希望对您有所帮助。
public static void main(String[] args) throws ConfigurationException {
Configurations configurations = new Configurations();
XMLConfiguration xmlConfiguration = configurations.xml("config.xml");
Map<String, Map<String, String>> map = new HashMap<>();
xmlConfiguration.getNodeModel().getRootNode().getChildren().forEach(x -> {
Map<String, String> temp = new HashMap<>();
x.getChildren().forEach(y -> {
temp.put(y.getAttributes().get("key").toString(), y.getValue().toString());
});
map.put(x.getNodeName(), temp);
});
System.err.println(map);
}
您的示例的输出地图:
{
anotherMap={
favoriteDay=wednesday,
favoriteColor=green,
favoriteSeason=winter,
favoriteFruit=orange
},
map={
favoriteDay=today,
favoriteSeason=summer,
favoriteFruit=pomegranate}
}
这里我正在创建地图,其中将包含两个对应于键 map
和 anothermap
的地图,根据您的示例 xml.
我遇到的问题与 this question 中的问题几乎相同。假设我有一个类似的 xml 文件,我想阅读第一张地图:
<?xml version="1.0" encoding="UTF-8"?>
<root>
<map>
<entry key="favoriteSeason">summer</entry>
<entry key="favoriteFruit">pomegranate</entry>
<entry key="favoriteDay">today</entry>
</map>
<anotherMap>
<entry key="favoriteSeason">winter</entry>
<entry key="favoriteFruit">orange</entry>
<entry key="favoriteDay">wednesday</entry>
<entry key="favoriteColor">green</entry>
</anotherMap>
</root>
注意:键值对的数量可能会有所不同。
使用 ConfigurationNode 的链接问题中的答案很好,但在 2.5 版本中 class 不存在。在 Apache's user's guide 之后,我想到了这样的事情:
Configurations configurations = new Configurations();
XMLConfiguration xmlConfiguration = configurations.xml("config.xml");
Map<String, String> map = new HashMap<>();
int pairsCount = xmlConfiguration.getList(String.class, "map.entry").size();
for(int i = 0; i < pairsCount; ++i)
map.put(xmlConfiguration.getString("map.entry(" + i + ")[@key]"), xmlConfiguration.getString("map.entry(" + i + ")"));
我觉得获取键值对的数量并不是最好的,而且将键值对放在地图中也不是那么可读。有更好的方法吗?
希望对您有所帮助。
public static void main(String[] args) throws ConfigurationException {
Configurations configurations = new Configurations();
XMLConfiguration xmlConfiguration = configurations.xml("config.xml");
Map<String, Map<String, String>> map = new HashMap<>();
xmlConfiguration.getNodeModel().getRootNode().getChildren().forEach(x -> {
Map<String, String> temp = new HashMap<>();
x.getChildren().forEach(y -> {
temp.put(y.getAttributes().get("key").toString(), y.getValue().toString());
});
map.put(x.getNodeName(), temp);
});
System.err.println(map);
}
您的示例的输出地图:
{
anotherMap={
favoriteDay=wednesday,
favoriteColor=green,
favoriteSeason=winter,
favoriteFruit=orange
},
map={
favoriteDay=today,
favoriteSeason=summer,
favoriteFruit=pomegranate}
}
这里我正在创建地图,其中将包含两个对应于键 map
和 anothermap
的地图,根据您的示例 xml.