Timster
Timster

Reputation: 1

Marshall xsd:any content as string with JAXB

I am searching for an solution for the following problem.

I have xml content which looks like this:

<my:Chapter>
     <my:Number>8.1.</my:Number>
     <my:Title>chapter title</my:Title>
     <my:Text>
         <div xmlns="http://www.w3.org/1999/xhtml">dsfsdfsda sadfa ef aw</div>
         <div xmlns="http://www.w3.org/1999/xhtml">aawfwa ef</div>
         <div xmlns="http://www.w3.org/1999/xhtml">aw</div>
     </my:Text>
 </my:Chapter>

The node "Text" can contain any xhtml elements. When marshalling this xml to java class, I need this text content as it is as a string.

The XSD for that element looks like this:

<xsd:element name="Text">
    <xsd:complexType mixed="true">
        <xsd:sequence>
            <xsd:any minOccurs="0" maxOccurs="unbounded" namespace="http://www.w3.org/1999/xhtml"
                     processContents="lax"/>
        </xsd:sequence>
    </xsd:complexType>
</xsd:element>

The JAXB class generation is producing the following:

@XmlRootElement(name = "Chapter")
public class Chapter {

    @XmlElement(name = "Number")
    protected String number;
    @XmlElement(name = "Title")
    protected String title;
    @XmlElement(name = "Text")
    protected Text text;
    
    getters and setters ...

}


@XmlRootElement(name = "Text")
public class Text {

    @XmlMixed
    @XmlAnyElement(lax = true)
    protected List<Object> content;

    public List<Object> getContent() {
        if (content == null) {
            content = new ArrayList<Object>();
        }
        return this.content;
    }

}

Currently all child elements of the Text element are marshalled as List<Object>, which needs to be unmarshalled again to get the xhtml content as string. This is in my eyes a waste of processing time.

What I want is, that the xhtml text content is just taken as a single String. Like so:

@XmlRootElement(name = "Chapter")
public class Chapter {

    @XmlElement(name = "Number")
    protected String number;
    @XmlElement(name = "Title")
    protected String title;
    @XmlElement(name = "Text")
    protected String text;
    
    getters and setters ...

}

I already have setup some xjb bindings and XmlAdapter for some other cases and expect that also as a solution for this, but I was not successful to achieve what I want so far.

The XML files can not be changed, because it is already production data. XSD and bindings are under my control and can be changed.

Does anyone have an idea?

Upvotes: 0

Views: 557

Answers (0)

Related Questions