JAXB - Unmarshalling polymorphic objects

后端 未结 1 1040
迷失自我
迷失自我 2021-01-13 07:29

I\'m given XML that looks like (lots more attributes of course):


  
  

        
相关标签:
1条回答
  • 2021-01-13 07:58

    There area couple of different approaches:

    JAXB (JSR-222)

    The following approach should work with any JAXB implementation (Metro, MOXy, JaxMe, etc). Use an XmlAdapter where the adapted object contains the properties of the parent class and all the subclasses. In the XmlAdapter add the logic of when a particular subclass should be used. For an example see the link to a similar question below:

    • Java/JAXB: Unmarshall XML attributes to specific Java object attributes

    EclipseLink JAXB (MOXy)

    You could use the @XmlDescriminatorNode extension in EclipseLink JAXB (MOXy) to handle this use case.

    Check out my answer to a similar question:

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

    We improved this support in the EclipseLink 2.2 release:

    • http://bdoughan.blogspot.com/2010/11/jaxb-and-inheritance-moxy-extension.html
    0 讨论(0)
提交回复
热议问题