javax.xml.bind.UnmarshalException: Unexpected elem

2019-08-13 22:09发布

问题:

I am doing this,

JAXBContext jaxbContext = JAXBContext.newInstance(new Class[] { 
      mine.beans.ObjectFactory.class }); 
Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
orderhistory = (OrderHistory) unmarshaller.unmarshal(new StreamSource(
      new StringReader(responseXML)));`

I am getting javax.xml.bind.UnmarshalException: Unexpected element "OrderHistory". Expected elements are "{_http://orderhistory.shc.com/common/domain}OrderHistory". but i checked my OrderHistory.java i have the

@XmlRootElement(name = "OrderHistory")
public class OrderHistory{

What am i missing???

Even the package-info.java file is also present

Here is my response xml,
<?xml version="1.0" encoding="UTF-8"?>
<OrderHistory>
<guid>5555</guid>
<syNumber xsi:nil="true"></syNumber>
<email xsi:nil="true"></email>
<totalPages>0</totalPages>
</OrderHistory>

Still i am facing the same issue???

I ve made changes to my package-info.java i have removed the namespace attribute but still i am seeing the same issue,

@javax.xml.bind.annotation.XmlSchema() package mine.beans;

回答1:

It appears as though your input document is not namespace qualified.

You have:

<OrderHistory>...</OrderHistory>

And your JAXB (JSR-222) implementation is expecting:

<OrderHistory xmlns="_http://orderhistory.shc.com/common/domain">...</OrderHistory>

Related

If you are unmarshalling from a DOM, make sure to call setNamespaceAware(true) on the instance of DocumentBuilderFactory.

For More Information

  • http://blog.bdoughan.com/2010/08/jaxb-namespaces.html


回答2:

As hint. Try to marshal the document from your object, and see if the tags are written as expected.



回答3:

Did you try to modify your XML? Your UNmarshaller is expecting the OrderHistory-Element to be part of the "http://orderhistory.shc.com/common/domain" namespace, and yet it isnt. You could give this a try:

<?xml version="1.0" encoding="UTF-8"?>
<OrderHistory xmlns="_http://orderhistory.shc.com/common/domain">
<guid>5555</guid>
<syNumber xsi:nil="true"></syNumber>
<email xsi:nil="true"></email>
<totalPages>0</totalPages>
</OrderHistory>