javax.xml.bind.UnmarshalException: 意外元素 (uri:"", local:"COUNTRY") 预期元素为 <{http://www.w3schools.com}COUNTRY>
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17319367/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
javax.xml.bind.UnmarshalException: unexpected element (uri:"", local:"COUNTRY") Expected elements are <{http://www.w3schools.com}COUNTRY>
提问by user2523808
Xml content is
XML 内容是
<?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
这是我的国家课程 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 Info:
包装信息:
@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 error like this:
我得到这样的错误:
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)
dont know where the fault is, I tried a lot, but I am unable to figure it out..
不知道是哪里出了问题,试了很多次,还是没弄明白。。
回答by bdoughan
Based on your mappings the JAXB impl expects the XML document to be namespace qualified. You can fix it in one of the following ways:
根据您的映射,JAXB impl 期望 XML 文档是命名空间限定的。您可以通过以下方式之一修复它:
Add the namespace qualification to the XML document.
<COUNTRY xmlns="http://www.w3dchools.com">Remove the namespace metadata from the JAXB mappings. You have specified it using the package level
@XmlSchemaannotation.Use an
XmlFilterto apply the correct namespace information to your current XML document.
将命名空间限定添加到 XML 文档。
<COUNTRY xmlns="http://www.w3dchools.com">从 JAXB 映射中删除名称空间元数据。您已使用包级别
@XmlSchema注释指定它。使用
XmlFilter将正确的名称空间信息应用到您当前的 XML 文档。

