-6ren"> -xml内容为 255258 bazarr Street 24-6ren">
gpt4 book ai didi

xml - javax.xml.bind.UnmarshalException : unexpected element (uri :"", local :"COUNTRY") 预期的元素是 <{http ://www. w3schools.com}COUNTRY>

转载 作者:数据小太阳 更新时间:2023-10-29 02:01:35 25 4
gpt4 key购买 nike

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>

这是我的乡村课国家.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;
}
}

包装信息:

@javax.xml.bind.annotation.XmlSchema(namespace = "http://www.w3schools.com",
elementFormDefault = javax.xml.bind.annotation.XmlNsForm.QUALIFIED)
package com.kk;

主类

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);
}

我遇到这样的错误:

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)

不知道哪里出了问题,我试了很多,但我无法弄清楚..

最佳答案

根据您的映射,JAXB impl 期望 XML 文档符合命名空间条件。您可以通过以下方式之一修复它:

  1. 将 namespace 限定添加到 XML 文档。

     <COUNTRY xmlns="http://www.w3dchools.com">
  2. 从 JAXB 映射中删除 namespace 元数据。您已使用包级别 @XmlSchema 注释指定它。

  3. 使用 XmlFilter 将正确的命名空间信息应用到您当前的 XML 文档。

关于xml - javax.xml.bind.UnmarshalException : unexpected element (uri :"", local :"COUNTRY") 预期的元素是 <{http ://www. w3schools.com}COUNTRY>,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17319367/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com