9

我正在尝试XML使用SimpleXML解析来解析以下内容。
我尝试了不同的方式来解析AttributesElement但无法成功解析下面的 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)
.
.
4

1 回答 1

9

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

请参阅 JavaDoc @Attribute

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

但是有一个很好且简单的解决方案:不要将元素和属性结合起来,而是创建一个执行此操作的类。

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 + '}';
        }
    }

}

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

但请注意类中没有参数的空构造函数Order(它是内部类)。它们是必需的。但是您可以制作所有这些private- 无需公开它们。唯一重要的是你一个没有参数的构造函数。

如何使用(示例)

File f = new File("whatever.xml");

Serializer ser = new Persister();
OrderObject orderObject = ser.read(OrderObject.class, f);

System.out.println(orderObject);

此代码从文件中解析 Xml 并打印反序列化的对象。

输入 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>

结果(来自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}}]}

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

于 2013-06-20T17:53:49.887 回答