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

后端 未结 5 2121

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


  

        
5条回答
  •  旧时难觅i
    2020-11-27 19:27

    There is @XmlSeeAlso annotation to tell to bind subclasses.

    For example, with the following class definitions:

     class Animal {}
     class Dog extends Animal {}
     class Cat extends Animal {}
    

    The user would be required to create JAXBContext as JAXBContext.newInstance(Dog.class,Cat.class) (Animal will be automatically picked up since Dog and Cat refers to it.)

    XmlSeeAlso annotation would allow you to write:

     @XmlSeeAlso({Dog.class,Cat.class})
     class Animal {}
     class Dog extends Animal {}
     class Cat extends Animal {}
    

提交回复
热议问题