users@jaxb.java.net

Re: Issue unmarshalling object...unexpected element

From: Kenny MacLeod <kennym_at_kizoom.com>
Date: Mon, 22 Oct 2007 08:50:14 +0100

the namespace is usually specified in an annotation in the
package-info.java file in the same package as the generated .java
files.... is that there?


Anil Arora wrote:
> I am upgrading our application to use JDK 6u3. Along with it, I’m
> upgrading from JAXB 1 to the JAXB implementation in JDK 6. However, I’m
> finding some strange issues when I am unmarshalling what should be
> simple xml. I’m using xjc to generate the java objects so I’m not doing
> anything funny with annotations. It seems that the namespace is messed
> up in the generated code.
>
>
>
>
>
> javax.xml.bind.UnmarshalException: unexpected element
> (uri:"http://schemas.interlacesystems.com/jaxbtest/JAXBTest.xsd",
> local:"Description"). Expected elements are <{}Description>
>
> at
> com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallingContext.handleEvent(UnmarshallingContext.java:523)
>
> at
> com.sun.xml.internal.bind.v2.runtime.unmarshaller.Loader.reportError(Loader.java:199)
>
> at
> com.sun.xml.internal.bind.v2.runtime.unmarshaller.Loader.reportError(Loader.java:194)
>
> at
> com.sun.xml.internal.bind.v2.runtime.unmarshaller.Loader.reportUnexpectedChildElement(Loader.java:71)
>
> at
> com.sun.xml.internal.bind.v2.runtime.unmarshaller.Loader.childElement(Loader.java:65)
>
> at
> com.sun.xml.internal.bind.v2.runtime.unmarshaller.StructureLoader.childElement(StructureLoader.java:209)
>
> at
> com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallingContext._startElement(UnmarshallingContext.java:366)
>
> at
> com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallingContext.startElement(UnmarshallingContext.java:347)
>
> at
> com.sun.xml.internal.bind.v2.runtime.unmarshaller.SAXConnector.startElement(SAXConnector.java:101)
>
> at
> org.apache.xerces.parsers.AbstractSAXParser.startElement(Unknown Source)
>
> at
> org.apache.xerces.impl.XMLNSDocumentScannerImpl.scanStartElement(Unknown
> Source)
>
> at
> org.apache.xerces.impl.XMLDocumentFragmentScannerImpl$FragmentContentDispatcher.dispatch(Unknown
> Source)
>
> at
> org.apache.xerces.impl.XMLDocumentFragmentScannerImpl.scanDocument(Unknown
> Source)
>
> …
>
>
>
>
>
> My xsd schema is...
>
>
>
> <?xml version="1.0" encoding="utf-8"?>
>
> <xsd:schema
>
>
> targetNamespace="http://schemas.interlacesystems.com/jaxbtest/JAXBTest.xsd"
>
> elementFormDefault="qualified"
>
> xmlns="http://schemas.interlacesystems.com/jaxbtest/JAXBTest.xsd"
>
> xmlns:xsd="http://www.w3.org/2001/XMLSchema"
>
> xmlns:jxb="http://java.sun.com/xml/ns/jaxb"
>
> xmlns:xjc="http://java.sun.com/xml/ns/jaxb/xjc"
>
> jxb:version="2.0"
>
> jxb:extensionBindingPrefixes="xjc">
>
>
>
> <xsd:annotation>
>
> <xsd:appinfo>
>
> <jxb:globalBindings generateIsSetMethod="false"
>
> typesafeEnumBase="xsd:string"
>
> collectionType="java.util.ArrayList"
>
> choiceContentProperty="true"
>
> generateValueClass="false">
>
> <xjc:serializable/>
>
> </jxb:globalBindings>
>
> <jxb:schemaBindings>
>
> <jxb:package name="com.interlacesystems.jaxbtest"/>
>
> </jxb:schemaBindings>
>
> </xsd:appinfo>
>
> </xsd:annotation>
>
>
>
> <xsd:complexType name="MyObject">
>
> <xsd:annotation>
>
> <xsd:documentation xml:lang="en">A ModelObject is a
> toplevel object
>
> imported into the system via XML or the Admin
> UI</xsd:documentation>
>
> </xsd:annotation>
>
> <xsd:sequence>
>
> <xsd:element name="Description" type="xsd:string"
> minOccurs="0"/>
>
> </xsd:sequence>
>
> <xsd:attribute name="id" type="xsd:long" use="optional"
> default="-1"/>
>
> <xsd:attribute name="name" type="xsd:string" use="required"/>
>
> </xsd:complexType>
>
>
>
> <xsd:element name="SampleObject1" type="SampleObject1Type"/>
>
> <xsd:complexType name="SampleObject1Type">
>
> <xsd:complexContent>
>
> <xsd:extension base="MyObject">
>
> <xsd:attribute name="active" type="xsd:boolean"
> use="required"/>
>
> </xsd:extension>
>
> </xsd:complexContent>
>
> </xsd:complexType>
>
> </xsd:schema>
>
>
>
>
>
>
>
>
>
>
>
> My code to test the unmarshalling is as follows…
>
>
>
> String SAMPLEOBJ1_XML =
>
> "<SampleObject1 " +
>
> "
> xmlns=\"http://schemas.interlacesystems.com/jaxbtest/JAXBTest.xsd\"" +
>
> " xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"" +
>
> " xsi:type=\"SampleObject1\"" +
>
> " name=\"sampleobject\"" +
>
> " active=\"true\">" +
>
> " <Description>This is a description</Description>" +
>
> "</SampleObject1>";
>
>
>
> JAXBContext jc = JAXBContext.newInstance(“com.interlacesystems.jaxbtest”);
>
> Unmarshaller unmarshaller = jc.createUnmarshaller();
>
> unmarshaller.setEventHandler(new DefaultValidationEventHandler());
>
> JAXBElement<?> obj = (JAXBElement<?>)unmarshaller.unmarshal(new
> InputSource(new StringReader(SAMPLEOBJ1_XML)));
>
>
>
>
>
> Is there something I’m doing that’s wrong?
>
>
>
> Anil
>