With JAXB how do we avoid JAXBElement for mixed complexType

205 Views Asked by At

When a complexType is marked with mixed="true" in XSD, the generated Java class produces a List with either a "String" type or a JAXBElement wrapper around the actual value. Without mixed="true", the JAXBElement is not produced and the value is directly mapped to the type class. Eg:

    <xs:complexType name="div" mixed="true">
        <xs:sequence minOccurs="0" maxOccurs="unbounded">
            <xs:element name="a" minOccurs="0" maxOccurs="unbounded" type="anchorType"/>
     </xs:sequence>
     ...
   </complexType>

Here the div type would contain a a List getContent() method that either returns a String or a JAXBElement for AnchorType.

I wanted it to return just either String or AnchorType without the JAXBElement.

1

There are 1 best solutions below

0
On

Consider this XML which contains a div tag and some child tags:

<parentTag>
  <div>myPlainTextValue<a/><a/></div>
</parentTag>

But the mixed content in an XML document does not necessarily occur in a single block. This XML is also valid according to the XSD:

<parentTag>
  <div>my<a/>Plain<a/>Text<a/>Value</div>
</parentTag>

In both cases, the text content of the tag 'div' is 'myPlainTextValue'. But in the second example it is distributed in 4 smaller chunks in between the child tags. JAXB needs to generate code that can handle the general case, so I would not expect it to have a special mode for your case.