javax.xml.bind.UnmarshalException: Unexpected element. What am i missing?

≡放荡痞女 提交于 2019-12-11 07:51:52

问题


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>


来源:https://stackoverflow.com/questions/12079052/javax-xml-bind-unmarshalexception-unexpected-element-what-am-i-missing

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!