简体   繁体   English

将多个Xml元素映射到单个类

[英]Mapping Multiple Xml Elements to Single Class

Let us say I have a Xml Like this 假设我有一个像这样的Xml

<person>
<dob>xxxx</dob>
<name>yyyyyy</name>
<phone>zzzzzz</phone>
</person>

<person1>
<dob>xxxx</dob>
<name>yyyyyy</name>
<phone>zzzzzz</phone>
</person1>

Is there a way where I Can map person and person1 data to a single class in java 有没有一种方法可以将person和person1数据映射到java中的单个类

My java code has two seperate classes for person and person1 now 我的Java代码现在为person和person1提供了两个单独的类

You can declare multiple elements in the annotation. 您可以在批注中声明多个元素。 eg: 例如:

@XmlElements({
    @XmlElement(name="person", type=Person.class),
    @XmlElement(name="person1", type=Person.class)
})
List<Person> getPersons() {
    return persons;
}

If both elements is available in one document, and you want to display both of them in as one class, you can combine the elements by yourself. 如果两个元素在一个文档中都可用,并且您希望将它们都显示为一个类,则可以自己组合这些元素。 eg: 例如:

@XmlElement(name = "person")
public List<Person> getPerson() {
    return person;
}

@XmlElement(name = "person1")
public List<Person> getPerson1() {
    return person1;
}

public List<Person> getPeople() {
    List<Person> people = new ArrayList<Person>();
    if(person != null) people.addAll(person);
    if(person1 != null) people.addAll(person1);
    return people;
}

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

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