java - Simple XML parse XML to List -
i use simple xml (simple-xml-2.6.2.jar) parse xml file like:
<?xml version="1.0" encoding="utf-8" ?> <orderlist> <order id="1"> <name>name1</name> </order> <order id="2"> <name>name2</name> </order> </orderlist>
the root element contains subelements. wanna arraylist, how it?
here's possible solution, hope helps you:
annotations of order
class:
@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 }
example
class, containing list:
@root(name="elementlist") public class example { @elementlist(required=true, inline=true) private list<order> list = new arraylist<>(); // ... }
and here's code reading code:
serializer ser = new persister(); example example = ser.read(example.class, file); // file = xml file // 'list' contains orders
Comments
Post a Comment