gpt4 book ai didi

java - 解码 xml 时出现 UnmarshalException

转载 作者:太空宇宙 更新时间:2023-11-04 15:11:46 27 4
gpt4 key购买 nike

异常javax.xml.bind.UnmarshalException:意外元素(uri:“http://services.forddirect.fordvehicles.com/searchTest”,本地:“ClassName”)。预期元素为(无)

我正在粘贴用于解码 xml 的代码部分

try {

File file = new File("ReflectionInput.xml");
JAXBContext jaxbContext = JAXBContext.newInstance(SearchTest.class);
Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
SearchTest className = (SearchTest) jaxbUnmarshaller
.unmarshal(file);
System.out.println(className);

} catch(JAXBException e) {
e.printStackTrace();
}

}

从 xml 解码时出现异常。

Exception javax.xml.bind.UnmarshalException: unexpected element (uri:"http://services.forddirect.fordvehicles.com/searchTest", local:"ClassName"). Expected elements are (none)
at com.sun.xml.bind.v2.runtime.unmarshaller.UnmarshallingContext.handleEvent(UnmarshallingContext.java:647)
at com.sun.xml.bind.v2.runtime.unmarshaller.Loader.reportError(Loader.java:258)
at com.sun.xml.bind.v2.runtime.unmarshaller.Loader.reportError(Loader.java:253)
at com.sun.xml.bind.v2.runtime.unmarshaller.Loader.reportUnexpectedChildElement(Loader.java:120)
at com.sun.xml.bind.v2.runtime.unmarshaller.UnmarshallingContext$DefaultRootLoader.childElement(UnmarshallingContext.java:1052)
at com.sun.xml.bind.v2.runtime.unmarshaller.UnmarshallingContext._startElement(UnmarshallingContext.java:483)
at com.sun.xml.bind.v2.runtime.unmarshaller.UnmarshallingContext.startElement(UnmarshallingContext.java:464)
at com.sun.xml.bind.v2.runtime.unmarshaller.SAXConnector.startElement(SAXConnector.java:152)
at org.apache.xerces.parsers.AbstractSAXParser.startElement(Unknown Source)

xsd 文件

<?xml version="1.0" encoding="utf-8"?>
<xs:schema xmlns="http://services.forddirect.fordvehicles.com/searchTest"
attributeFormDefault="unqualified" elementFormDefault="qualified"
targetNamespace="http://services.forddirect.fordvehicles.com/searchTest"
xmlns:xs="http://www.w3.org/2001/XMLSchema">

<xs:element name="SearchTest" type="searchTest">
</xs:element>
<xs:complexType name="searchTest">
<xs:sequence>
<xs:element name="ClassName" type="className" minOccurs="0"
maxOccurs="unbounded" />
</xs:sequence>
</xs:complexType>

<xs:complexType name="className">
<xs:sequence>
<xs:element name="param" type="xs:string" minOccurs="0"
maxOccurs="unbounded" />
</xs:sequence>
</xs:complexType>
</xs:schema>

xml 文件

     <?xml version="1.0" encoding="UTF-8"?>
<ClassName xmlns="http://services.forddirect.fordvehicles.com/searchTest">
<param name ="make">Ford</param>
<param name ="model">Mustang</param>
<param name ="year">2011</param>
<param name ="dealerPACode">2011</param>
<param name ="segment">car</param>
<param name ="segmentdefiner">NGP_ModelInfo_VehicleType</param>
<param name ="configtoken">Config[|Ford|Mustang|2014|1|1.|100A.P8A.....COU.~YZKAA.V6.LESS.]</param>
<param name ="vin">3FA6P0H79DR338449</param>
</ClassName>

最佳答案

问题是什么:

由于 SearchTest 是具有命名复杂类型的全局元素,因此不会为 SearchTest 类生成 @XmlRootElement 注释。相反,在 ObjectFactory 类上为其生成一个 @XmlElementDecl 注释。这是因为在 JAXB 中,类对应于复杂类型,并且命名的复杂类型可能有多个与其对应的全局元素。

  <xs:element name="SearchTest" type="searchTest">
</xs:element>
<xs:complexType name="searchTest">
<xs:sequence>
<xs:element name="ClassName" type="className" minOccurs="0"
maxOccurs="unbounded" />
</xs:sequence>
</xs:complexType>

如何修复它:

在从 XML 模式生成的模型上创建 JAXBContext 时,应在 ObjectFactory 类或生成模型的包名称上创建它,而不是在单独的模型上创建它模型类来获取所有必要的元数据。

JAXBContext jaxbContext = JAXBContext.newInstance(ObjectFactory.class);

了解更多信息

关于java - 解码 xml 时出现 UnmarshalException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21253529/

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