简体   繁体   English

JAXB解组XML,在子元素中具有父元素

[英]JAXB unmarshal XML with a parent element in a child element

I've looked and googled around but couldn't find an answer for my problem. 我已经四处寻找和搜寻,但找不到我的问题的答案。 I have an XML file with the following structure: 我有一个具有以下结构的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>

My Java files are the following: 我的Java文件如下:

Persons.java 人员.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 人.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 Children.java

public class Children {

    private List<Person> persons;

    /* getters and setters */
}

NOTE: I used annotations on the object variables because i didn't want to write down getters and setters. 注意:我在对象变量上使用了注释,因为我不想写下getter和setter。 I know, that otherwise a @XmlAccessorType(XmlAccessType.FIELD) is necessary. 我知道,否则@XmlAccessorType(XmlAccessType.FIELD)是必需的。

So <children> can actually have one or more <person> as a child. 因此, <children>实际上可以有一个或多个<person>作为孩子。 I think I got confused with the different Lists. 我想我对不同的列表感到困惑。 When I simply test it by unmarshalling the given XML file with a simple System.out.println() command on every person I see every element and attribute with the correct value but person.getChildren() always returns null. 当我通过使用简单的System.out.println()命令对每个人解组给定的XML文件来简单地测试它时,我看到每个元素和属性都具有正确的值,但是person.getChildren()始终返回null。 What did I do wrong? 我做错了什么?

In your Children class, you've forgotten to add 在您的儿童班上,您忘记添加

@XmlElement(name="person")

to the list of persons inside children class. 进入儿童班的人员名单。 So it should looks like this: 所以它应该看起来像这样:

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;
    }
}

After adding this my output is: 添加后,我的输出是:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM