users@jaxb.java.net

Issue unmarshalling object...unexpected element

From: Anil Arora <aarora_at_interlacesystems.com>
Date: Sun, 21 Oct 2007 21:14:40 -0700

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.h
andleEvent(UnmarshallingContext.java:523)

        at
com.sun.xml.internal.bind.v2.runtime.unmarshaller.Loader.reportError(Loa
der.java:199)

        at
com.sun.xml.internal.bind.v2.runtime.unmarshaller.Loader.reportError(Loa
der.java:194)

        at
com.sun.xml.internal.bind.v2.runtime.unmarshaller.Loader.reportUnexpecte
dChildElement(Loader.java:71)

        at
com.sun.xml.internal.bind.v2.runtime.unmarshaller.Loader.childElement(Lo
ader.java:65)

        at
com.sun.xml.internal.bind.v2.runtime.unmarshaller.StructureLoader.childE
lement(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.s
tartElement(UnmarshallingContext.java:347)

        at
com.sun.xml.internal.bind.v2.runtime.unmarshaller.SAXConnector.startElem
ent(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$FragmentContentDis
patcher.dispatch(Unknown Source)

        at
org.apache.xerces.impl.XMLDocumentFragmentScannerImpl.scanDocument(Unkno
wn Source)

        ...

 

 

My xsd schema is...

 

<?xml version="1.0" encoding="utf-8"?>

<xsd:schema

 
targetNamespace="http://schemas.interlacesystems.com/jaxbtest/JAXBTest.x
sd"

      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