Skip to main content

Need Help : javax.xml.bind.UnmarshalException: Unexpected element

5 replies [Last post]
rajakumar
Offline
Joined: 2005-04-26

Hi,
When I tried to unmarshal the XML file, I am getting the following exception.

javax.xml.bind.UnmarshalException: Unexpected element {schema location}:element name.

Please help me to solve this issue.

Rajakumar.

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
kohsuke
Offline
Joined: 2003-06-09

The format of the error message is {nsUri}:localName and that points to the element that the JAXB RI saw. The error happens when this element is not a valid element name in the current context according to the schema.

Check your XML document and make sure they are valid against your schema.

jgrahamthomas
Offline
Joined: 2005-05-25

I know this is an old post, but I recently started using JAXB 2.0 to marshal/unmarshal XML into and out of a web service interface which accepts and returns a java.lang.String object. This interface is provided by a third party vendor so we have no control over the method signature. I'd prefer to exchange complex objects instead of Strings, but what can you do?

I also experienced the "javax.xml.bind.UnmarshalException: Unexpected element..." exception at runtime when unmarshalling the response message from the web service.

I then realized that, for each target namespace in an XSD, JAXB (via the XJC script/ant task) creates a file called "package-info.java" which maps an XML namespace to a java package, i.e:

@javax.xml.bind.annotation.XmlSchema(namespace = "http://www.acme.au.com/xml/in")
package com.acme.clients.credit.authorisation.integration.data.request;

I was putting all of the JAXB-generated classes into the same java package which caused the package-info class to be overwritten (I had two XML namespaces in my XSDs, http://www.acme.au.com/xml/in and http://www.acme.au.com/xml/out). This meant that, at runtime, JAXB could not unmarshal the response XML string. Hence the UnmarshalException.

So, make sure each XSD targetnamespace has its own java package when generating the JAXB java classes. There may also be a way to map multiple namespaces in one package-info file, but I haven't looked into this yet.

kohsuke
Offline
Joined: 2003-06-09

XJC offers the "-npa" option if you don't want XJC to use package-info.java (which is a shared resource, as you point out.)

Using "-npa" prevents XJC to generate package-info.java, which in turns allows you to put multiple XJC outputs into one package.

kamleshjkl
Offline
Joined: 2006-12-27

Hi,

I need your help. I have similar error.

javax.xml.bind.UnmarshalException: unexpected element (uri:"", local:"orderList"). Expected elements are <{myURL}myElementB>,<{myURL}myElementD>

One of my xml schema element extends from another element. I don't know if this is what causing the error.



here is the Ant task that I have created. In the schema directory I have multiple xsd files. Child xsd files are referenced in the parent xsd file like using include


I am using JAXB RI 2.1.

Thanks

Message was edited by: kamleshjkl

kamleshjkl
Offline
Joined: 2006-12-27

After I added namespace information to the xml file, it worked out.