Skip to main content

JAXB marshal creates unwanted xml elements

No replies
Jeroen_van_Schelven
Offline
Joined: 2013-09-24
Points: 0

I am using xjc from the java SDK to create my JAXB classes from the following xsd file:

<xs:complexType name="customer">
    <xs:sequence>
        <xs:element name="customerinfo" type="information"/>
    </xs:sequence>
</xs:complexType>

<xs:complexType name="information">
        <xs:choice minOccurs="1" maxOccurs="1">
            <xs:element name="infos" type="informations"/>
            <xs:element name="addressline" type="address"/>
            <xs:element name="string" type="xs:string"/>
        </xs:choice>
</xs:complexType>

<xs:complexType name="informations">
        <xs:choice minOccurs="1" maxOccurs="unbounded">
            <xs:element name="infos" type="informations"/>
            <xs:element name="addressline" type="address"/>
            <xs:element name="string" type="xs:string"/>
        </xs:choice>
</xs:complexType>

<xs:complexType name="address"/>

As you can see it has some recursion in it with the "infos" element. When i create my classes with xjc and make an instance of these classes as follows:

ObjectFactory objFact = new ObjectFactory();
    Customer customer = objFact.createCustomer();

    Information infoRoot = objFact.createInformation();
    Information infoInside = objFact.createInformation();
    Information infoInside2 = objFact.createInformation();
    Informations infos = objFact.createInformations();
    Address addressInside = objFact.createAddress();
    infos.getInfosOrAddresslineOrString().add(infoInside);
    infos.getInfosOrAddresslineOrString().add(addressInside);
    infos.getInfosOrAddresslineOrString().add("bla");
    Informations infosInside = objFact.createInformations();
    infosInside.getInfosOrAddresslineOrString().add(infoInside2);
    infoInside.setInfos(infosInside);
    infoRoot.setInfos(infos);

    customer.setCustomerinfo(infoRoot);

    JAXBElement<Customer> jaxbCustomer = objFact.createCustomer(customer);

    JAXBContext jc = JAXBContext.newInstance(Customer.class);
    Marshaller marshaller = jc.createMarshaller();
    marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
    marshaller.marshal(jaxbCustomer, System.out);

The output is not as i would expect it to be:

<customer>
<customerinfo>
    <infos>
        <addressline xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="information">
            <infos>
                <addressline xsi:type="information"/>
            </infos>
        </addressline>
        <addressline/>
        <string>bla</string>
    </infos>
</customerinfo>
</customer>

As you can see there is an extra element around the infos object that was never instantiated in the code.
Why is the addressline with type information element added to the marhsalled xml?