异常javax.xml.bind.UnmarshalException:意外元素(uri:“http://services.forddirect.fordvehicles.com/searchTest”,local:“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>
答案 0 :(得分:0)
由于SearchTest
是具有命名复杂类型的全局元素,因此不会为@XmlRootElement
类生成SearchTest
注释。而是在@XmlElementDecl
类上为其生成ObjectFactory
注释。这是因为在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 Schema生成的模型上创建JAXBContext
时,您应该在ObjectFactory
类或生成的模型的包名称上创建它而不是单个模型类来获取所有必要的元数据。
JAXBContext jaxbContext = JAXBContext.newInstance(ObjectFactory.class);