4

I am using gradle to generate Java classes based on an XML Schema file. I am using 'org.glassfish.jaxb:jaxb-xjc:2.2.11' and 'org.glassfish.jaxb:jaxb-runtime:2.2.11' as dependencies so I can use the 'com.sun.tools.xjc.XJC2Task' class to generate the classes.

This is the schema file:

<?xml version="1.0" encoding="utf-8"?>
<xs:schema id="test"
           targetNamespace="urn:oio:records:1.0.0"
           xmlns="urn:oio:records:1.0.0"
           xmlns:xs="http://www.w3.org/2001/XMLSchema"
           elementFormDefault="qualified">

    <xs:element name="records" type="recordsType"/>
    <xs:element name="record" type="recordType"/>

    <xs:complexType name="recordsType">
        <xs:sequence>
            <xs:element ref="record" minOccurs="0" maxOccurs="unbounded"/>
        </xs:sequence>
    </xs:complexType>
    <xs:complexType name="recordType">
        <xs:attribute name="key" type="xs:string"/>
        <xs:attribute name="value" type="xs:string"/>
    </xs:complexType>
</xs:schema>

One of the generated classes look like this:

import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlType;

@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "recordType")
public class RecordType {
    @XmlAttribute(name = "key")
    protected String key;
    @XmlAttribute(name = "value")
    protected String value;

    public String getKey() {return key;}

    public void setKey(String value) {this.key = value;}

    public String getValue() {return value;}

    public void setValue(String value) {this.value = value;}
}

How can I change the name value in the @XmlType annotation? I would like it to be

@XmlType(name = "record")

I have tried using a bindingsfile and tried to experiment with the <javaType> tag in the bindingsfile, but without luck.

EDIT: The reason I need to change this is that I need to split an XML file using the stax splitter from Camel (http://camel.apache.org/stax.html section called "Iterate over a collection using JAXB and StAX"). This looks at the name attribute of the @XmlType annotation to recognize the xml tag to split on in the file. The recognized tag (<record>) will then be JAXB parsed to a RecordType java class.

Mr Lister
  • 45,515
  • 15
  • 108
  • 150
npeder
  • 788
  • 10
  • 26

2 Answers2

0

The name in the @XmlType annotation is the name of the complexType in your schema file. This is how the parameter 'name' is defined for this annotation.

So, if you want to change it, you have to change the name of the complexType in your schema:

<xs:complexType name="record">
    <xs:attribute name="key" type="xs:string"/>
    <xs:attribute name="value" type="xs:string"/>
</xs:complexType>
Tobo
  • 1
  • 1
  • I forgot to mention that I can not change the schema files. Added some more information to the question about why I need this. – npeder Apr 20 '16 at 06:43
0

You can use the jaxb2-annotate-plugin to override the value of the name attribute in the generated Java class.

For your code, it would look like this:

<xs:schema id="test"
           targetNamespace="urn:oio:records:1.0.0"
           xmlns="urn:oio:records:1.0.0"
           xmlns:xs="http://www.w3.org/2001/XMLSchema"
           xmlns:annox="http://annox.dev.java.net"
           jaxb:extensionBindingPrefixes="annox"
           elementFormDefault="qualified">

    <xs:element name="records" type="recordsType"/>
    <xs:element name="record" type="recordType"/>

    <xs:complexType name="recordsType">
        <xs:sequence>
            <xs:element ref="record" minOccurs="0" maxOccurs="unbounded"/>
        </xs:sequence>
    </xs:complexType>
    <xs:complexType name="recordType">
        <xs:annotation>
            <xs:appinfo>
                <annox:annotate target="class">
                    @javax.xml.bind.annotation.XmlType(name = "record")
                </annox:annotate>
            </xs:appinfo>
        </xs:annotation>
        <xs:attribute name="key" type="xs:string"/>
        <xs:attribute name="value" type="xs:string"/>
    </xs:complexType>
</xs:schema>

I don't know if this should be considered a hack, but it does the trick. Interestingly enough, in my case even the namespace attribute of XmlType is still generated and filled with the same value as without adding that explicit annotation.

Hein Blöd
  • 1,553
  • 1
  • 18
  • 25