简体   繁体   English

将具有相同元素和属性名称的XML映射到Java对象

[英]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.

相关问题 通用 object 的 Map 的 order 元素 — 通过 object 的通用属性 — Lamda - ZD52387880E79EA22817A72 - Order element of a Map of generic object — by a generic attribute of the object — Lamda - Java 具有包名称的java对象的Xml元素 - Xml Element to java Object which has package names 在Java中的XML文档中按属性查找元素,并在同一元素中获取另一个属性的值 - Find a Element by attribute in a XML Document in Java and get value of another attribute in the same element Java 比较 object 元素,如果“名称”属性相同,则打印一次 - Java compare object element and if "name" attribute is same print it once Java+Jackson+XML:将一个java对象属性序列化为同名的XML元素 - Java+Jackson+XML: serialize a java object properties as XML elements with same names 将Jackson json属性映射到相应的xml元素 - Map Jackson json attribute to corresponding xml element Jackson XmlMapper map XML 嵌套元素的属性 - Jackson XmlMapper map XML attribute of nested element 使用标记名称的多个选项将XML映射到Java - Map XML to Java with multiple option for tag names 相同结构中的动态XML元素名称 - Dynamic XML Element Names in the same Structure JAXB:使用同一元素的多个名称解组xml - JAXB: unmarshalling xml with multiple names for the same element
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM