[英]Map XML with same element and attribute names to Java object
I already searched for this particular issue, the closest thread i found was this one: Java/JAXB: Unmarshall XML elements with same name but different attribute values to different class members But it's still not exactly what i need, so i hope someone can help me with this. 我已经搜索了这个特定问题,发现的最接近的线程是: Java / JAXB:取消编组具有相同名称但不同类成员具有不同属性值的XML元素,但这仍然不是我真正需要的,所以我希望有人可以提供帮助我这个。
I am doing a SOAP request on a Zimbra Collaboration Suite 7 Server to get a contact. 我正在Zimbra Collaboration Suite 7服务器上进行SOAP请求以获取联系。 The response is something like this:
响应是这样的:
<cn fileAsStr="Arthur, Spooner" f="" id="280" rev="1973" d="1338524233000" t="" md="1338524233" ms="1973" l="7"><meta/><a n="homePostalCode">93849</a><a n="lastName">Spooner</a><a n="birthday">1980-05-24</a><a n="homeStreet">Berkleystreet 99</a><a n="firstName">Arthur</a></cn>
I want to map this to a Java object, something like this: 我想将此映射到Java对象,如下所示:
public class Contact {
Integer id;
Integer rev;
String namePrefix;
String firstName;
String middleName;
String lastName;
String jobTitle;
ArrayList<Adress> adresses;
Date birthday;
String department;
Integer mobilePhone;
String email;
String company;
String notes;
...
I usually do this using JAXB, but as all the elements are called a and all the attributes n, I don't know how to map this. 我通常使用JAXB来执行此操作,但是由于所有元素都称为a且所有属性都称为n,所以我不知道如何映射此内容。 I really would appreciate a code snippet or any kind of help.
我真的很感谢代码片段或任何形式的帮助。 Thanks in advance.
提前致谢。
You could try doing something like this: 您可以尝试执行以下操作:
@XmlAccessorType(XmlAccessType.FIELD)
public class ContactAttribute {
@XmlAttribute(name="n")
private String attribute;
@XmlValue
private String value;
}
@XmlRootElement(name = "cn")
@XmlAccessorType(XmlAccessType.FIELD)
public class Contact {
@XmlAttribute
Integer id;
@XmlAttribute
Integer rev;
//...
@XmlElements(@XmlElement(name = "a"))
List<ContactAttribute> attributes;
//...
}
使用Castor映射 ,它将帮助您编组和解组数据。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.