Java/JAXB: Unmarshall Xml to specific subclass based on an attribute

后端 未结 5 2119

Is it possible to use JAXB to unmarshall xml to a specific Java class based on an attribute of the xml?


  

        
5条回答
  •  生来不讨喜
    2020-11-27 19:25

    The annotation @XmlElements enables you to specify which tag corresponds with which subclass.

    @XmlElements({
        @XmlElement(name="square", type=Square.class),
        @XmlElement(name="triangle", type=Triangle.class)
    })
    public List getShape() {
        return shape;
    }
    

    Also see javadoc for @XmlElements

提交回复
热议问题