JAXB unmarshal exception "unexpected root element"

I have a very simple xsd and just one xml file I am trying to unmarshal. I switched to jws-2.0 and am still getting the same exception I go when using jws-1.5. I would be very grateful for any assistance on this:

Here is my main program:

public static void main(String[] args) {

getCancelPenalty();

}

public static void getCancelPenalty() {

String jaxbContext = "generated";

String cancelPenaltyFilename = "CancelPenaltyOne.xml";

JAXBContext jc = null;

Unmarshaller u = null;

CancelPenalty cancelPenalty = null;

try {

jc = JAXBContext.newInstance(jaxbContext);

u = jc.createUnmarshaller();

cancelPenalty = (CancelPenalty) u.unmarshal(

new FileInputStream(cancelPenaltyFilename));

} catch (Exception e) {

e.printStackTrace();

System.exit(0);

}

int updateTUID = cancelPenalty.getUpdateTUID();

System.out.println("These values were retrieved from " +

"the Cancel Penalty object:");

System.out.println("UpdateTUID: "+ updateTUID);

}

Here is my xsd file:

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

<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">

<xsd:complexType name="CancelPenalty">

<xsd:sequence>

<xsd:element name="UpdateTUID" type="xsd:int"/>

</xsd:sequence>

</xsd:complexType>

</xsd:schema>

Here is my xml file that i am trying to parse:

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

<CancelPenalty>

<UpdateTUID>1</UpdateTUID>

</CancelPenalty>

And finally here is stack trace of exception:

javax.xml.bind.UnmarshalException: unexpected element (uri:"", local:"CancelPenalty"). Expected elements are (unknown)

at com.sun.xml.bind.v2.runtime.unmarshaller.UnmarshallingContext.handleEvent(UnmarshallingContext.java:523)

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

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

at com.sun.xml.bind.v2.runtime.unmarshaller.Loader.reportUnexpectedChildElement(Loader.java:71)

at com.sun.xml.bind.v2.runtime.unmarshaller.UnmarshallingContext$DefaultRootLoader.childElement(UnmarshallingContext.java:920)

at com.sun.xml.bind.v2.runtime.unmarshaller.UnmarshallingContext._startElement(UnmarshallingContext.java:364)

at com.sun.xml.bind.v2.runtime.unmarshaller.UnmarshallingContext.startElement(UnmarshallingContext.java:345)

at com.sun.xml.bind.v2.runtime.unmarshaller.SAXConnector.startElement(SAXConnector.java:117)

at com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.startElement(Unknown Source)

at com.sun.org.apache.xerces.internal.impl.XMLNSDocumentScannerImpl.scanStartElement(Unknown Source)

at com.sun.org.apache.xerces.internal.impl.XMLNSDocumentScannerImpl$NSContentDispatcher.scanRootElementHook(Unknown Source)

at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl$FragmentContentDispatcher.dispatch(Unknown Source)

at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanDocument(Unknown Source)

at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(Unknown Source)

at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(Unknown Source)

at com.sun.org.apache.xerces.internal.parsers.XMLParser.parse(Unknown Source)

at com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.parse(Unknown Source)

at com.sun.xml.bind.v2.runtime.unmarshaller.UnmarshallerImpl.unmarshal0(UnmarshallerImpl.java:200)

at com.sun.xml.bind.v2.runtime.unmarshaller.UnmarshallerImpl.unmarshal(UnmarshallerImpl.java:173)

at javax.xml.bind.helpers.AbstractUnmarshallerImpl.unmarshal(AbstractUnmarshallerImpl.java:137)

at javax.xml.bind.helpers.AbstractUnmarshallerImpl.unmarshal(AbstractUnmarshallerImpl.java:184)

at populateDB.Populate.getCancelPenalty(Populate.java:28)

at populateDB.Populate.main(Populate.java:15)

Any ideas what is going awry?

[4196 byte] By [matthewabqa] at [2007-11-26 14:35:07]
# 1

Solved:

My xsd file was incorrect (I guess):

instead of:

<xs:complexType name="CancelPenalty">

I needed to use:

<xsd:element name="CancelPenalty">

<xsd:complexType>

Not sure why the first construct is incorrect, but I found another example of an xsd file that used the second construct and it worked for me as well.

matthewabqa at 2007-7-8 8:15:55 > top of Java-index,Enterprise & Remote Computing,Enterprise Technologies...