Skip to main content

JAXB marshal creates unwanted xml elements

Please note these forums are being decommissioned and use the new and improved forums at
No replies
Joined: 2013-09-24

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

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

<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: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: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();
    Informations infosInside = objFact.createInformations();


    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:

        <addressline xmlns:xsi="" xsi:type="information">
                <addressline xsi:type="information"/>

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?