Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/356.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用ApacheCommons配置(java)加载xml文件?_Java_Xml_Apache Commons - Fatal编程技术网

如何使用ApacheCommons配置(java)加载xml文件?

如何使用ApacheCommons配置(java)加载xml文件?,java,xml,apache-commons,Java,Xml,Apache Commons,这是我的java项目结构 src/main/java |_LoadXml.java src/main/resources/ |_config.xml src/test/java src/test/resources 我想使用apache公共配置库加载以下xml文件 <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE properties SYSTEM "http://java.sun.com/dtd/properties.

这是我的java项目结构

src/main/java
  |_LoadXml.java
src/main/resources/
  |_config.xml
src/test/java
src/test/resources
我想使用apache公共配置库加载以下xml文件

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE properties SYSTEM "http://java.sun.com/dtd/properties.dtd">
<properties>
<comment>Here are some favorites</comment>
<entry key="favoriteSeason">summer</entry>
<entry key="favoriteFruit">pomegranate</entry>
<entry key="favoriteDay">today</entry>
</properties>

我想将xml键和值加载到一个带有由“.”(点)分隔的层次结构节点的映射中。如果有人能在这方面帮助我,那将非常有帮助。

将xml键和值加载到
映射中

    public static Map<String, String> parseConfig() throws ConfigurationException {

        XMLConfiguration config = new XMLConfiguration("config.xml");
        NodeList list = config.getDocument().getElementsByTagName("entry");

        Map<String, String> map = new HashMap<String, String>();
        for (int i = 0; i < list.getLength(); i++) {
            Node node = list.item(i);
            String key = node.getAttributes().getNamedItem("key").getTextContent();
            String val = node.getTextContent();
            map.put(key, val);
        }
        System.out.println(map);
        return map;
    }
public static Map parseConfig()引发ConfigurationException{
XMLConfiguration config=newxmlconfiguration(“config.xml”);
NodeList list=config.getDocument().getElementsByTagName(“条目”);
Map Map=newhashmap();
对于(int i=0;i
输出

{favoriteSeason=summer,favoriteFruit=石榴,favoriteDay=today}
只需使用
config.getRootNode()
然后使用
node.getChildren(“条目”)


你能举一个你想要的输出的例子吗?因为您已经有了一个映射,所以可以使用
config.getString(“FavoriteSearch”)
获得一个值。XMLConfiguration没有一个接受字符串参数的构造函数。@luis.espinal不再有了,但它可能是OP提问时使用的版本。
    public static Map<String, String> parseConfig() throws ConfigurationException {

        XMLConfiguration config = new XMLConfiguration("config.xml");
        NodeList list = config.getDocument().getElementsByTagName("entry");

        Map<String, String> map = new HashMap<String, String>();
        for (int i = 0; i < list.getLength(); i++) {
            Node node = list.item(i);
            String key = node.getAttributes().getNamedItem("key").getTextContent();
            String val = node.getTextContent();
            map.put(key, val);
        }
        System.out.println(map);
        return map;
    }
XMLConfiguration config = new XMLConfiguration("_config.xml");
Map<String, String> configMap = new HashMap<String, String>();
ConfigurationNode node = config.getRootNode();
for (ConfigurationNode c : node.getChildren("entry"))
{
    String key = (String)c.getAttribute(0).getValue();
    String value = (String)c.getValue();
    configMap.put(key, value);
}
System.out.println(configMap.get("favoriteSeason")); // prints: summer