javajaxbmaven-jaxb2-pluginjaxb2-basics

JAXB Generating unexpected tag name, returns type name


I have the issue that the XML I'm generating does not match what I would expect.

I have the following XSD...

<xs:element name="DOSLog" type="DOSLogType"/>
<xs:complexType name="DOSLogType">
    <xs:sequence>
        <xs:element name="Transaction" type="DOSLogTransaction"/>
    </xs:sequence>
</xs:complexType>

When I generate the XML from these JAXB objects I get the following XML...

<DOSLogType>
    <Transaction/>
</DOSLogType>

I was not expecting the outputted tag to be <DOSLogType>, but to be <DOSLog>.

The code is returning an object of type DOSLogType.java

@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(
  name = "DOSLogType",
  propOrder = {"transaction"}
)
@XmlRootElement(
  name = "DOSLogType"
)
public class DOSLogType implements Cloneable, CopyTo, Equals, HashCode, ToString {

I was hoping that I could control the generated Object and XML tag name with the bindings.xjc This does not seem to work though.

<jaxb:bindings scd="~arts:DOSLogType"><jaxb:class ref="org.doslog.bean.DOSLogType" /></jaxb:bindings>

Can anyone suggest how I can control XML that is generated?

===================================

There were two solutions to this, the XSD approach shown below. The second is to fix it in the bindings.xjc

<jaxb:bindings xmlns:jaxb="http://java.sun.com/xml/ns/jaxb"
    xmlns:xjc="http://java.sun.com/xml/ns/jaxb/xjc"
    xmlns:xs="http://www.w3.org/2001/XMLSchema"
    xmlns:inheritance="http://jaxb2-commons.dev.java.net/basic/inheritance"
    xmlns:annox="http://annox.dev.java.net"
    jaxb:extensionBindingPrefixes="inheritance annox"
    jaxb:version="2.1">

<jaxb:bindings schemaLocation="DOSLog.xsd" node="/xs:schema">
    <jaxb:bindings node="xs:complexType[@name='DOSLogType']">
        <annox:annotate>
            <annox:annotate annox:class="javax.xml.bind.annotation.XmlRootElement" name="DOSLog"/>
        </annox:annotate>
    </jaxb:bindings>

Solution

  • You can use inline binding in xsd itself

    <xs:element name="DOSLog" type="DOSLogType"/>
    <xs:complexType name="DOSLogType">
    <xs:annotation>
                        <xs:documentation>Some DOSLog type documentatioms.</xs:documentation>
                        <xs:appinfo>
                                <annox:annotate>
                                    <annox:annotate annox:class="javax.xml.bind.annotation.XmlRootElement" name="DOSLog"/>
                                </annox:annotate>
                                <jaxb:class name="DOSLogType"/>
                            </xs:appinfo>
                        </xs:annotation>
        <xs:sequence>
            <xs:element name="Transaction" type="DOSLogTransaction"/>
        </xs:sequence>
    </xs:complexType>