How to load xml file using apache commons configuration (java)?

后端 未结 2 2146
一生所求
一生所求 2021-01-15 04:04

This is my java project strucutre

src/main/java
  |_LoadXml.java
src/main/resources/
  |_config.xml
src/test/java
src/test/resources

I want

2条回答
  •  有刺的猬
    2021-01-15 04:45

    Just use the config.getRootNode() and then node.getChildren("entry")

    XMLConfiguration config = new XMLConfiguration("_config.xml");
    Map configMap = new HashMap();
    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);
    }
    

    Then you can just do:

    System.out.println(configMap.get("favoriteSeason")); // prints: summer
    

提交回复
热议问题