users@jaxb.java.net

Unmarshalling complex types (without global elements) in JAXB1

From: Aleksei Valikov <valikov_at_gmx.net>
Date: Mon, 21 Jan 2008 19:19:46 +0100

Hi.

I've finally managed to solve the problem I've been having for a long
time already.

The task is about unmarshalling complex types from XML fragments.
Imagine we have a complex type like this one:

        <xsd:complexType name="CI_ResponsibleParty">
                <xsd:sequence>
                        <xsd:element name="individualFirstName" type="base:CharacterString"
minOccurs="0" ref:id="375"/>
                        <xsd:element name="individualLastName" type="base:CharacterString"
minOccurs="0" ref:id="375"/>
                        <xsd:element name="organisationName" type="base:CharacterString"
minOccurs="0" ref:id="376"/>
                        <xsd:element name="positionName" type="base:CharacterString"
minOccurs="0" ref:id="377"/>
                        <xsd:element name="contactInfo" type="base:CI_Contact"
minOccurs="0" ref:id="378"/>
                </xsd:sequence>
        </xsd:complexType>

Assume we don't have a global element for this type. The question (or
task) is - how to unmarshall an instance of this type from an XML
fragment like this:

<any uuid="12" xmlns="http://www.disy.de/preludio2/base">
        <individualFirstName>none</individualFirstName>
        <positionName>test</positionName>
        <contactInfo>
                <hoursOfService>10-16</hoursOfService>
        </contactInfo>
</any>

Name of the root element is not important. JAXB1 can't unmarshal this
fragment without global element, so even if you know which type is
being unmarshalled, you can't do it with standard API. Typical task
is: you'd like to load a part of your object structure from XML, but
there's no global element for the corresponding complex type.

With this mail, I wanted to share my solution. Source code is attached.

Bye.
/lexi