jeff porter
jeff porter

Reputation: 6620

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>

Upvotes: 0

Views: 423

Answers (2)

Alexandr Ivanov
Alexandr Ivanov

Reputation: 399

You can set root element name while generating XML string with marshaller.
To do this create JAXBElement for your object of DOCLogType type.
Something like this:

protected Marshaller marshaller;
protected <T> String marshal(T xmlBean, Class<T> clazz, String rootElementNamespace, String rootElementName) {
    StringWriter stringWriter = new StringWriter();

    try {
        JAXBElement<T> jaxbElement = new JAXBElement<T>(new QName(rootElementNamespace, rootElementName), clazz,
                xmlBean);

        marshaller.marshal(jaxbElement, stringWriter);
    } catch (Exception e) {
        log.error(e);
        throw new RuntimeException(e);
    }

    return stringWriter.toString();
}

Upvotes: 1

Optional
Optional

Reputation: 4507

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>

Upvotes: 1

Related Questions