JAXB2: Mapping nested elements into the same Java class

后端 未结 2 1191
温柔的废话
温柔的废话 2021-01-18 22:02

I\'m having trouble trying to map nested elements into the same Java class.

XML

What I\'m trying to do here is to set id attrib

相关标签:
2条回答
  • 2021-01-18 22:42

    If you use EclipseLink JAXB (MOXy) then you can leverage the @XmlPath extension for this (I'm the MOXy tech lead):

    import javax.xml.bind.annotation.XmlAccessType;
    import javax.xml.bind.annotation.XmlAccessorType;
    import javax.xml.bind.annotation.XmlRootElement;
    
    import org.eclipse.persistence.oxm.annotations.XmlPath;
    
    @XmlRootElement
    @XmlAccessorType(XmlAccessType.FIELD)
    public class SlideText extends Slide {
    
        @XmlPath("layout/text/text()")
        private String  text;
    
    }
    
    • http://bdoughan.blogspot.com/2010/09/xpath-based-mapping-geocode-example.html

    Using standard JAXB you could leverage an XmlAdapter:

    • http://bdoughan.blogspot.com/2010/07/xmladapter-jaxbs-secret-weapon.html
    0 讨论(0)
  • 2021-01-18 22:45

    Add a new class Layout:

    public class SlideText extends Slide {
        @XmlElement
        private Layout layout;
    }
    
    public class Layout {
        @XmlAttribute
        private String  text;
    }
    
    0 讨论(0)
提交回复
热议问题