JAXB使用子元素中的父元素解组XML

时间:2016-05-17 11:26:18

标签: java xml jaxb unmarshalling

我看了一眼,然后用Google搜索,但找不到我的问题的答案。我有一个XML文件,其结构如下:

<!DOCTYPE persons [
    <!ELEMENT lastname (#PCDATA)>
    <!ELEMENT firstname (#PCDATA)>
    <!ELEMENT residence (#PCDATA)>
    <!ELEMENT children (person*)>
    <!ELEMENT person (firstname, (lastname?), (children?), (residence?))>
    <!ELEMENT persons (person+)>
    <!ATTLIST person id ID #REQUIRED>
    <!ATTLIST person friends IDREFS #IMPLIED>
    <!ATTLIST persons file CDATA #IMPLIED>
    ]>

<persons file="this">
    <person id="p-1">
        <firstname>Homer</firstname>
        <lastname>Simpson</lastname>
        <residence>Springfield</residence>
        <children>
            <person id="p-3">
                <firstname>Bart</firstname>
                <residence>Springfield</residence>
            </person>
        </children>
    </person>
    <person id="p-2" friends="p-1">
        <firstname>Ned</firstname>
        <lastname>Flanders</lastname>
        <residence>Springfield</residence>
    </person>
</persons>

我的Java文件如下:

Persons.java

@XmlRootElement(name="persons")
public class Persons {

    @XmlAttribute(name="file")  
    private String file;
    @XmlElement(name="person")
    private List<Person> persons;

    /* setters and getters */
}

Person.java

public class Person {

    @XmlAttribute(name="id")
    private String id;
    @XmlAttribute(name="friends")
    private String friends;
    private String firstname;
    private String lastname;
    private String residence;
    private List<Children> children;

    /* getters and setters */
}

Children.java

public class Children {

    private List<Person> persons;

    /* getters and setters */
}

注意:我在对象变量上使用了注释,因为我不想写下getter和setter。我知道,否则@XmlAccessorType(XmlAccessType.FIELD)是必要的。

所以<children>实际上可以有一个或多个<person>作为孩子。 我想我对不同的列表感到困惑。 当我通过在每个人上使用简单的System.out.println()命令解组给定XML文件来测试它时,我看到每个元素和属性都具有正确的值,但person.getChildren()始终返回null。我做错了什么?

1 个答案:

答案 0 :(得分:1)

在您的儿童课程中,您已忘记添加

@XmlElement(name="person")

到儿童班内的人员名单。所以看起来应该是这样的:

public class Children {

    private List<Person> persons;

    public List<Person> getPersons() {
        return persons;
    }

    @XmlElement(name="person") // MISSING ANNOTATION
    public void setPersons(List<Person> persons) {
        this.persons = persons;
    }
}

添加后我的输出是:

Persons = [
    file: this, 
    persons: [
        Person = [
            id=p-1, 
            freinds=null, 
            firstName=Homer, 
            lastName=Simpson, 
            residence=Springfield, 
            children=[
                Children = [
                    persons=[
                        Person = [
                            id=p-3, 
                            freinds=null, 
                            firstName=Bart, 
                            lastName=null, 
                            residence=Springfield, 
                            children=null
                        ]
                    ]
                ]
            ]
        ], 
        Person = [
            id=p-2, 
            freinds=p-1, 
            firstName=Ned, 
            lastName=Flanders, 
            residence=Springfield, 
            children=null
        ]
    ]
]