Exception "javax.xml.bind.UnmarshalException: unexpected element ..." thrown when mapping XML to Java using JAXB
Issue
- We are using JAXB to map XML to Java objects in my application like the following:
String xml = ...
JAXBContext context = JAXBContext.newInstance("com.example.app.model");
Unmarshaller unmarshaller = context.createUnmarshaller();
unmarshaller.unmarshal(IOUtils.toInputStream(xml, "UTF-8"));
- However, we get the following
"javax.xml.bind.UnmarshalException: unexpected element ..."
exception:
Caused by: javax.xml.bind.UnmarshalException: unexpected element (uri:"http://model.app.example.com", local:"MyData1"). Expected elements are <{}MyData1>,<{}MyData2>,<{}MyData3>[, ...]
at com.sun.xml.bind.v2.runtime.unmarshaller.UnmarshallingContext.handleEvent(UnmarshallingContext.java:642)
at com.sun.xml.bind.v2.runtime.unmarshaller.Loader.reportError(Loader.java:254)
at com.sun.xml.bind.v2.runtime.unmarshaller.Loader.reportError(Loader.java:249)
at com.sun.xml.bind.v2.runtime.unmarshaller.Loader.reportUnexpectedChildElement(Loader.java:116)
at com.sun.xml.bind.v2.runtime.unmarshaller.UnmarshallingContext$DefaultRootLoader.childElement(UnmarshallingContext.java:1049)
at com.sun.xml.bind.v2.runtime.unmarshaller.UnmarshallingContext._startElement(UnmarshallingContext.java:478)
at com.sun.xml.bind.v2.runtime.unmarshaller.UnmarshallingContext.startElement(UnmarshallingContext.java:459)
at com.sun.xml.bind.v2.runtime.unmarshaller.InterningXmlVisitor.startElement(InterningXmlVisitor.java:71)
at com.sun.xml.bind.v2.runtime.unmarshaller.SAXConnector.startElement(SAXConnector.java:148)
at com.sun.xml.bind.unmarshaller.DOMScanner.visit(DOMScanner.java:239)
at com.sun.xml.bind.unmarshaller.DOMScanner.scan(DOMScanner.java:122)
at com.sun.xml.bind.unmarshaller.DOMScanner.scan(DOMScanner.java:105)
at com.sun.xml.bind.v2.runtime.unmarshaller.UnmarshallerImpl.unmarshal0(UnmarshallerImpl.java:317)
at com.sun.xml.bind.v2.runtime.unmarshaller.UnmarshallerImpl.unmarshal(UnmarshallerImpl.java:297)
at com.example.app.actions.MyAction.process(MyAction.java:106)
... 14 more
Environment
- Red Hat JBoss Enterprise Application Platform (EAP)
- 5
- 6
- Red Hat JBoss SOA Platform (SOA-P)
- 5
- JAXB
Subscriber exclusive content
A Red Hat subscription provides unlimited access to our knowledgebase, tools, and much more.