JAXB 使用 child 元素中的 parent 元素解组 XML
JAXB unmarshal XML with a parent element in a child element
我四处查看和搜索,但找不到解决我的问题的方法。我有一个具有以下结构的 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 */
}
注意:我在 object 变量上使用了注释,因为我不想写下 getter 和 setter。我知道,否则 @XmlAccessorType(XmlAccessType.FIELD)
是必要的。
所以<children>
实际上可以有一个或多个<person>
作为一个child。
我想我对不同的列表感到困惑。
当我通过对每个人使用简单的 System.out.println()
命令解组给定的 XML 文件来简单地测试它时,我看到每个元素和属性都具有正确的值,但 person.getChildren()
总是 returns null .我做错什么了?
在您的 Children class 中,您忘记添加
@XmlElement(name="person")
到 children class 内的人员列表。所以它应该是这样的:
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
]
]
]
我四处查看和搜索,但找不到解决我的问题的方法。我有一个具有以下结构的 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 */
}
注意:我在 object 变量上使用了注释,因为我不想写下 getter 和 setter。我知道,否则 @XmlAccessorType(XmlAccessType.FIELD)
是必要的。
所以<children>
实际上可以有一个或多个<person>
作为一个child。
我想我对不同的列表感到困惑。
当我通过对每个人使用简单的 System.out.println()
命令解组给定的 XML 文件来简单地测试它时,我看到每个元素和属性都具有正确的值,但 person.getChildren()
总是 returns null .我做错什么了?
在您的 Children class 中,您忘记添加
@XmlElement(name="person")
到 children class 内的人员列表。所以它应该是这样的:
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
]
]
]