Question

I am generating model classes from an xsd using the maven jaxb2 plugin. The xsd specifies certain elements with type=xs:token as follows:

<xs:element name="medium_text" type="xs:token"/>

This results in the following annotations being added to the field in the generated model:

@XmlJavaTypeAdapter(CollapsedStringAdapter.class)
@XmlSchemaType(name="token")
protected String medium_text;

The problem is the CollapsedStringAdapter removes new lines etc (which is the correct behaviour for xs:token) but I want to avoid this.

How can remove the xs:token type from the element or change it to xs:string using JAXB bindings? The easy solution is to remove the type from the xsd but is it possible using bindings without modifying the xsd?

Was it helpful?

Solution

You can use the following in your binding file.

<jxb:bindings schemaLocation = "schema.xsd">
   <jxb:bindings node = "//xs:element[@name='medium_text']">
             <jxb:javaType name="java.lang.String"/> 
   </jxb:bindings>
</jxb:bindings>

This results in the following:

@XmlElement(name = "mid_text", required = true)
@XmlJavaTypeAdapter(Adapter1 .class)
@XmlSchemaType(name = "token")
protected String midText;

Adapter1 however is harmless and does not change the string value.

Also if you want to do this to all your tokens you can add a globalBinding

  <jxb:globalBindings>
            <jxb:javaType name="java.lang.String"
                xmlType="xs:token" />
  </jxb:globalBindings>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top