2

I've a requirement to bind the XML content to String attribute of my pojo, and for that I've created my custom DomHandler to extract required part, something like below

<sample>
<color>red</color>
<content>
     <p>here is content <b>with bold</b></p>
</content>
</sample>

Which will map to pojo

@XmlRootElement
class Sample {
    @MyCustomAnnotation(value="abcde")
    @XmlElement(name="color")
    private String color;

    @MyCustomAnnotation(value="12345")
    @XmlElement(name="content")
    @XmlAnyElement(ContentHandler.class)
    private String content; 
}

I'm generating my pojos using XSD, also I've couple of custom annotations to be added to generated pojos so for that I'm using maven-jaxb2-plugin.

The problem is, it adds annotations as required along with @XmlElement which is mutually exclusive to @XmlAnyElement, is there any way to avoid adding @XmlElement annotation using XSD?

James Z
  • 12,209
  • 10
  • 24
  • 44
Akhil
  • 1,184
  • 1
  • 18
  • 42

1 Answers1

1

You can use xjc:dom to tell XJC that you want to handle this element as a DOM element. Example:

<jaxb:bindings 
    schemaLocation="http://schemas.opengis.net/owc/0.3.1/owsContext.xsd" 
    node="/xs:schema">

    <jaxb:schemaBindings>
        <jaxb:package name="net.opengis.owc.v_0_3_1"/>
    </jaxb:schemaBindings>

    <jaxb:bindings node="xs:complexType[@name='LayerType']//xs:element[@ref='kml:Document']">
        <xjc:dom/>
    </jaxb:bindings>

</jaxb:bindings>

lexicore
  • 42,748
  • 17
  • 132
  • 221
  • using above the XmlElement got removed but the issue now is the plugin is not able to add my custom DomHandler to annotation and I get exception "It is attached to a wrong place, or its inconsistent with other bindings" – Akhil Oct 16 '17 at 09:10
  • above happens because I'm trying to update XmlAnyElement with my custom DomHandler. – Akhil Oct 16 '17 at 09:28
  • For instance I've below in my bindings.xjb – Akhil Oct 16 '17 at 17:25