使用JAXB类进行解组时出错

时间:2017-10-23 00:39:16

标签: java xml jaxb unmarshalling

我已经从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)
    ....

有什么问题?

2 个答案:

答案 0 :(得分:0)

我不知道你的java类是怎么样的,但是你没有用@XMLRootElement注释根类的错误信息。

我使用MeetUpData类更改了JAXB的实例化,并使用@XMLRootElement注释它。

以下是功能来源

<强> MeetUpData.class

import java.io.Serializable;
import java.util.List;

import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement
public class MeetUpData implements Serializable {

    private static final long serialVersionUID = 1L;
    String location;
    String name;
    List<SessionData> sessionsData;

    public MeetUpData() {
        super();
    }

    public String getLocation() {
        return location;
    }

    public void setLocation(String location) {
        this.location = location;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public List<SessionData> getSessionsData() {
        return sessionsData;
    }

    public void setSessionsData(List<SessionData> sessionsData) {
        this.sessionsData = sessionsData;
    }

    @Override
    public String toString() {
        return "MeetUpData [location=" + location + ", name=" + name + ", sessionData=" + sessionsData + "]";
    }

}

要解组的代码

JAXBContext jc = JAXBContext.newInstance(MeetUpData.class);
Unmarshaller ums = jc.createUnmarshaller();
MeetUpData meetUpData = (MeetUpData) ums.unmarshal(new FileInputStream(
               "src\\com\\bases\\java\\InitialXML.xml"));
System.out.println("Info: " + meetUpData.toString());

此外,您需要更改yout XML中的“MeetUpData”标记,以小写“m”开头,如下所示:

<?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>

如果这不适合您,请提供您的课程。

答案 1 :(得分:0)

只尝试最后两行:

require'
C:/RailsInstaller/Ruby2.3.3/lib/ruby/gems/2.3.0/gems/bundler-1.15.3/lib/bundler.rb:108:in