How to read properties from xml file with java?

后端 未结 7 1398
独厮守ぢ
独厮守ぢ 2021-01-08 00:51

I have the following xml file:


    
        
        

        
7条回答
  •  梦谈多话
    2021-01-08 01:37

    I would just use JAXB to bind data into set of objects that have structure similar to your XML document.

    Something like:

    @XmlRootElement("resources")
    public class Resources {
      public List resource = new ArrayList(); // important, can't be left null
    }
    public class Resource {
      @XmlAttribute public String id;
      public List property;
    }
    // and so on
    

    one possible gotcha is regarding List serialization; there are two modes, wrapped and unwrapped; in your case, you want "unwrapped". Javadocs for annotations should show annotation to define this.

提交回复
热议问题