我想解组这个XML:
<result>
<Contacts>
<row nb="1">
<PK val="First Name">JeanValJean</PK>
<PK val="Last Name">Dupont</PK>
</row>
</Contacts>
</result>
我试图以这种方式取消它:
@XmlRootElement
public class Result {
@XmlElement(name = "Contacts")
private Contacts contacts;
然后:
@XmlRootElement
public class Contacts {
@XmlElement(name = "row")
private List<Contact> contacts = new ArrayList<>();
public Contacts() {}
public Contacts(Collection<Contact> contacts) {
this.contacts.addAll(contacts);
}
public Contacts add(Contact contact) {
contact.setNb(contacts.size() + 1);
contacts.add(contact);
return this;
}
然后(可能错误在这里 - 匹配PK?):
@XmlRootElement
public class Contact {
@XmlAttribute
private int nb = 0;
@XmlElement(name = "PK")
private Field firstName = new Field("First Name");
@XmlElement(name = "PK")
private Field lastName = new Field("Last Name");
然后:
@XmlRootElement(name = "PK")
public class Field {
@XmlAttribute(name = "val")
private String name;
@XmlValue
private String value;
当我试图解组时,我得到空名称值。
如何让它发挥作用? 如何简化?