Javax.xml.bind.UnmarshalException: unexpected element (uri: ", local:" COUNTRY ") Expected elements: <{http://www.w3schools.com} COUNTRY>
Xml content
<?xml version="1.0" encoding="UTF-8"?> <COUNTRY xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.w3schools.com Country_Details.xsd"> <STATE name="AndhraPradesh"> <DISTRICT name="Chittoor"> <PHONENO>255258</PHONENO> <ADDRESS>bazarr Street</ADDRESS> </DISTRICT> <DISTRICT name="Kadapa"> <PHONENO>24137457</PHONENO> <ADDRESS>congtres Street</ADDRESS> </DISTRICT> </STATE> <STATE> ... </STATE> </COUNTRY> This is my country class. Country.java
@XmlAccessorType(XmlAccessType.FIELD) @XmlType(name = " ", propOrder = { "state" }) @XmlRootElement(name = "COUNTRY") public class COUNTRY { @XmlElement(name = "STATE", required = true) protected List<Districts> state; public List<Districts> getSTATE() { if (state == null) { state = new ArrayList<Districts>(); } return this.state; } } Package Information:
@javax.xml.bind.annotation.XmlSchema(namespace = "http://www.w3schools.com", elementFormDefault = javax.xml.bind.annotation.XmlNsForm.QUALIFIED) package com.kk; Main class
public class App { public static void main(String[] args) throws JAXBException { JAXBContext context = JAXBContext.newInstance(AddressDetails.class, COUNTRY.class, Details.class, Districts.class, ObjectFactory.class); Unmarshaller um = context.createUnmarshaller(); JAXBElement<COUNTRY> jaxb = (JAXBElement<COUNTRY>) um .unmarshal(new File("src//Country.xml")); COUNTRY value = jaxb.getValue(); System.out.println(value); } I got an error:
Exception in thread "main" javax.xml.bind.UnmarshalException:unexpected element(uri:"", local:"COUNTRY"). Expected elements are <{http://www.w3schools.com}COUNTRY> 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.SAXConnector.startElement(SAXConnector.java:148) at com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.startElement(Unknown Source) at javax.xml.bind.helpers.AbstractUnmarshallerImpl.unmarshal(Unknown Source) at javax.xml.bind.helpers.AbstractUnmarshallerImpl.unmarshal(Unknown Source) at javax.xml.bind.helpers.AbstractUnmarshallerImpl.unmarshal(Unknown Source) at javax.xml.bind.helpers.AbstractUnmarshallerImpl.unmarshal(Unknown Source) at App.main(App.java:20) I donβt know where the error is, I tried a lot, but I can not understand it.
Based on your mappings, JAXB impl expects the XML document to have a namespace. You can fix this in one of the following ways:
Add the namespace qualification to the XML document.
<COUNTRY xmlns="http://www.w3dchools.com">Remove namespace metadata from JAXB mappings. You specified it using the
@XmlSchemapackage level annotation.Use
XmlFilterto apply the correct namespace information to the current XML document.