【发布时间】:2017-10-23 00:39:15
【问题描述】:
我已经从以下 xsd 模式生成了 JAXB 类:
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="MeetUpData">
<xs:complexType>
<xs:sequence>
<xs:element maxOccurs="unbounded" name="SessionData">
<xs:complexType>
<xs:attribute name="Date" type="xs:string" use="required" />
<xs:attribute name="Quantity" type="xs:int" use="required" />
<xs:attribute name="Theme" type="xs:string" use="required" />
</xs:complexType>
</xs:element>
</xs:sequence>
<xs:attribute name="Name" type="xs:string" use="required" />
<xs:attribute name="Location" type="xs:string" use="required" />
</xs:complexType>
</xs:element>
</xs:schema>
现在我正在尝试解组以下 XML 文档
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<MeetUpData>
<location>3</location>
<name>2</name>
<sessionsData>
<date>345</date>
<quantity>3</quantity>
<theme>Windows</theme>
</sessionsData>
<sessionsData>
<date>3</date>
<quantity>7</quantity>
<theme>9</theme>
</sessionsData>
</MeetUpData>
通过以下代码:
JAXBContext jc = JAXBContext.newInstance(MeetUp.class);
Unmarshaller ums = jc.createUnmarshaller();
meetUpData = (MeetUpData) ums.unmarshal(new FileInputStream("src\\com\\bases\\java\\InitialXML.xml"));
System.out.println("Info: " + meetUpData.toString());
但是我得到了一堆错误:
javax.xml.bind.UnmarshalException: unexpected element (uri:"", local:"MeetUpData"). Expected elements are (none)
at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallingContext.handleEvent(UnmarshallingContext.java:726)
....
有什么问题?
【问题讨论】:
-
您的 XML 与架构不匹配。例如,
Location应该是MeetUpData元素的一个属性。在您的 XML 中,您有一个单独的location元素。Name/name也是如此。此外,在 XML 中您有一个sessionsData元素,而在 XSD 中它被命名为SessionData,而且还有更多错误。确保您的 XML 与您的 XSD 匹配。
标签: java xml jaxb unmarshalling