我使用Simple
XML(simple-xml-2.6.2.jar)来解析xml文件,如:
<?xml version="1.0" encoding="UTF-8" ?> <orderList> <order id="1"> <name>NAME1</name> </order> <order id="2"> <name>NAME2</name> </order> </orderList>
根元素包含subElements.
我想成为ArrayList,怎么做?
解决方法
这是一个可能的解决方案,希望它可以帮助您:
Order类的注释:
@Root(name="order") public class Order { @Attribute(name="id",required=true) private int id; @Element(name="name",required=true) private String name; public Order(int id,String name) { this.id = id; this.name = name; } public Order() { } // Getter / Setter }
示例类,包含列表:
@Root(name="elementList") public class Example { @ElementList(required=true,inline=true) private List<Order> list = new ArrayList<>(); // ... }
Serializer ser = new Persister(); Example example = ser.read(Example.class,file); // file = your xml file // 'list' now contains all your Orders