[英]How to map complex XML element to Java class property using JAXB
I need to map my XML snippet onto Java class using JAXB, but have a tricky case. 我需要使用JAXB将我的XML片段映射到Java类,但是有一个棘手的情况。 I have the following XML: 我有以下XML:
<person>
<name part="first">Richard</name>
<name part="last">Brooks</name>
</person>
and need to map it onto the following class 并需要将它映射到下面的类
public class Person {
private String firstName;
private String lastName;
}
Could you please help me to figure out JAXB annotations to make it possible? 你能帮我弄清楚JAXB注释是否可行?
Here is one approach you could take, but would require you to create a separate class for Name: 这是您可以采用的一种方法,但需要您为Name创建一个单独的类:
@XmlRootElement
public class Person {
@XmlElement(name="name")
private List<Name> names;
...
}
public class Name {
@XmlAttribute
private String part; //would be set to "first" or "last"
@XmlValue
private String nameValue;
...
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.