I’m having issues unmarshalling XML that has been validated against a schema.
I have a large schema provided to me that I have extended. Here’s a snippet of what’s provided:
(Namespace “original”)
<xs:element name="Platform" type="PlatformType" abstract="true" />
<xs:complexType name="PlatformType" abstract="true">
...
</xs:complexType>
<xs:element name="SpringPlatform" type="SpringPlatformType" substitutionGroup="Platform" />
<xs:complexType name="SpringPlatformType">
<xs:complexContent>
<xs:extension base="PlatformType">
...
</xs:extension>
</xs:complexContent>
</xs:complexType>
Now I’ve extended SpringPlatformType with some additional functionality:
<xsd:element name="MySpringPlatform" type="MySpringPlatformType"
substitutionGroup="original:SpringPlatform" />
<xsd:complexType name="MySpringPlatformType">
<xsd:complexContent>
<xsd:extension base="original:SpringPlatformType">
<xsd:sequence>
...
</xsd:sequence>
</xsd:extension>
</xsd:complexContent>
</xsd:complexType>
I am receiving a list of Platforms, like this:
<xs:element name="PlatformList" type="PlatformListType" />
<xs:complexType name="PlatformListType">
<xs:sequence>
<xs:element ref="Platform" maxOccurs="unbounded"/>
</xs:sequence>
</xs:complexType>
I can correctly unmarshall incoming messages that use the original:SpringPlatform elements, but when I try to unmarshall my derived/extended types (MySpringPlatform), I get null. However, the XML validates against my schemas using XMLSpy 2011 just fine.
Any suggestions? I am using JAXB (Metro) 2.2.4 on Java 6u24, Windows XP SP3.
Thanks,
Brian
When you create a JAXB unmarshaller, you need to pass it the context; that is, the package of the structure you’re unmarshalling. We were using a utility class that made it easy for us to do this: pass it the XML string and a package name, and you get back an Object.
However, when unmarshalling structures that span across multiple namespaces and packages, you need to provide the package name for every package referenced, otherwise the unmarshaller doesn’t have the right context. I never knew you could pass multiple package names to the JAXB context.
WRONG:
RIGHT:
Maybe this was a stupid mistake, but I figured I would share just in case. Reading a bit on this site provided me with the idea. lexicore above also alluded to this, but I thought he was talking about classpath issues; all of the generated classes were always available to the context, however the context wasn’t being told to look for them.