如何用SimpleXMLparsingXML

我正在试图parsing下面的XML使用SimpleXMLparsing。
我已经尝试了不同的方法来parsingElementAttributes ,但无法成功parsing下面的XML。
它会产生一个列表在底部的错误。

 <prestashop xmlns:xlink="http://www.w3.org/1999/xlink"> <order> <id>1</id> <id_address_delivery xlink:href="http://abc.com/add/1">1</id_address_delivery> <id_address_invoice xlink:href="http://abc.com/add/2">2</id_address_invoice> </order> </prestashop> 

Order.java

 @Root(name="order") @Namespace(reference="http://www.w3.org/1999/xlink",prefix="xlink") public class Order { @Element(name="id",required=true) private int order_id; @Element(name="id_address_delivery",required=false) private int id_address_delivery; @Attribute( name="href", required=false) private String id_address_delivery_href; @Element(name="id_address_invoice",required=false) private int id_address_invoice; @Attribute(name="href", required=false) private String id_address_invoice_href; } 

OrderObject.java

 public class OrderObject { @ElementList(required=true, inline=true) private List<Order> list = new ArrayList<Order>(); public List<Order>getList() { return this.list; } } 

我得到的例外是:

 WARN/System.err(988): org.simpleframework.xml.core.PersistenceException: Duplicate annotation of name 'href' on field 'id_address_delivery_href' private java.lang.String com.prestashop.orders.Order.id_address_delivery_href at org.simpleframework.xml.core.StructureBuilder.process(StructureBuilder.java:250) at org.simpleframework.xml.core.StructureBuilder.process(StructureBuilder.java:173) at org.simpleframework.xml.core.ObjectScanner.field(ObjectScanner.java:438) at org.simpleframework.xml.core.ObjectScanner.scan(ObjectScanner.java:371) at org.simpleframework.xml.core.ObjectScanner.<init>(ObjectScanner.java:82) . . 

Solutions Collecting From Web of "如何用SimpleXMLparsingXML"

这个例外的原因是你的两个@Attribute注释。 它们没有设置为下一个元素,而是设置为整个Order对象。 您的注释将设置两次href ,但具有不同的值。

请参阅@Attribute JavaDoc:

Attribute注释表示XML元素中的可序列化的XML属性。 […]

但有一个很好的和简单的解决scheme:而不是组合元素和属性,做一个这样做的类。

OrderObject类:

 @Root(name = "prestashop") @Namespace(reference = "http://www.w3.org/1999/xlink", prefix = "xlink") public class OrderObject { @ElementList(required = true, inline = true) private List<Order> list; public OrderObject() { this.list = new ArrayList<>(); } public List<Order> getList() { return list; } // ... @Override public String toString() { return "OrderObject{" + "list=" + list + '}'; } } 

注意:这些类中的toString()方法仅用于检查结果!

Order类:

 @Root(name = "order") public class Order { @Element(name = "id") private int id; @Element(name = "id_address_delivery") private AdressDelivery delivery; @Element(name = "id_address_invoice") private AdressInvoice invoice; public Order(int id, AdressDelivery delivery, AdressInvoice invoice) { this.id = id; this.delivery = delivery; this.invoice = invoice; } private Order() { } // Getter / Setter etc. @Override public String toString() { return "Order{" + "id=" + id + ", delivery=" + delivery + ", invoice=" + invoice + '}'; } @Root() public static class AdressDelivery { @Namespace(reference = "http://www.w3.org/1999/xlink", prefix = "xlink") @Attribute(name = "href", required = false) private String link; @Text() private int value; public AdressDelivery(String link, int value) { this.link = link; this.value = value; } AdressDelivery() { } // Getter / Setter etc. @Override public String toString() { return "AdressDelivery{" + "link=" + link + ", value=" + value + '}'; } } @Root() public static class AdressInvoice { @Attribute(name = "href", required = false) @Namespace(reference = "http://www.w3.org/1999/xlink", prefix = "xlink") private String link; @Text() private int value; public AdressInvoice(String link, int value) { this.link = link; this.value = value; } AdressInvoice() { } // Getter / Setter etc. @Override public String toString() { return "AdressInvoice{" + "link=" + link + ", value=" + value + '}'; } } } 

您可以看到结合了属性和元素的AdressDeliveryAdressInvoice类。 你不必把它们作为内部类来实现; 随意把它们写成“正常”的。 你也不必把它们公之于众,甚至私有也是可能的(例如,在Order的构造函数中构造它们)。

但请注意Order类中没有参数的空构造函数(以及它的内部类)。 他们是必需的 。 但是你可以把它们全部private – 不需要暴露它们。 只有你一个没有参数的构造函数才是重要的。

如何使用(示例)

 File f = new File("whatever.xml"); Serializer ser = new Persister(); OrderObject orderObject = ser.read(OrderObject.class, f); System.out.println(orderObject); 

此代码从文件parsingXml并打印反序列化的对象。

inputXml (如你的问题)

 <prestashop xmlns:xlink="http://www.w3.org/1999/xlink"> <order> <id>1</id> <id_address_delivery xlink:href="http://abc.com/add/1">1</id_address_delivery> <id_address_invoice xlink:href="http://abc.com/add/2">2</id_address_invoice> </order> </prestashop> 

结果(来自println()

 OrderObject{list=[Order{id=1, delivery=AdressDelivery{link=http://abc.com/add/1, value=1}, invoice=AdressInvoice{link=http://abc.com/add/2, value=2}}]} 

没有明确的代表,但足以看到结果:-)