simple-xml的使用列子

simple-xml是一个xml和对象互转操作包,官方地址在这里:
http://simple.sourceforge.net/
上面提供了详细的说明和操作方法,非常强大。
这里转载下熔岩同学提供的例子,自己也从这里学到了很多,:

Java代码  收藏代码
  1. package test1;
  2. import org.simpleframework.xml.*;
  3. import org.simpleframework.xml.core.Persister;
  4. import java.io.File;
  5. import java.util.List;
  6. import java.util.ArrayList;
  7. @Root
  8. public class Example {
  9.         @Element
  10.         private String text;
  11.         @Attribute
  12.         private int index;
  13.         @Element()
  14.         private boolean flag;
  15.         @Element(required = false)
  16.         private Integer num;
  17.         @ElementList(required = false)
  18.         private List<String> slist = new ArrayList<String>();
  19.         public Example() {
  20.                 super();
  21.         }
  22.         public Example(String text, int index) {
  23.                 this.text = text;
  24.                 this.index = index;
  25. //                slist.add(“sdf”);
  26.         }
  27.         public String getMessage() {
  28.                 return text;
  29.         }
  30.         public int getId() {
  31.                 return index;
  32.         }
  33.         public static void main(String[] args) throws Exception {
  34.                 Serializer serializer = new Persister();
  35.                 Example example = new Example(“Example message”, 123);
  36.                 File result = new File(“outxml/example.xml”);
  37.                 serializer.write(example, result);
  38.                 Example _obj = serializer.read(Example.class, result);
  39.                 System.out.println(_obj.getMessage());
  40.         }
  41. }

输入的文件内容:
<example index=”123″>
<text>Example message</text>
<flag>false</flag>
<slist/>
</example>

控制台输出信息:
Example message

Process finished with exit code 0

第二个例子:

Java代码  收藏代码
  1. @Root
  2. public class Customer {
  3.         @Attribute(required = true)
  4.         private Long id;
  5.         @Element(required = true)
  6.         private String name;
  7.         @Element(required = false)
  8.         private Address address;
  9.         @ElementList(required = false,    type = Order.class)
  10.         private List<Order> orderList = new ArrayList<Order>(0);
  11.         public Customer() {
  12.         }
  13.         public Customer(Long id, String name) {
  14.                 this.id = id;
  15.                 this.name = name;
  16.         }
  17. @Root
  18. public class Address {
  19.         @Element(required = true)
  20.         private String code;
  21.         @Element
  22.         private String street;
  23.         @Element
  24.         private boolean isopen;
  25.         public Address() {
  26.         }
  27.         public Address(String code, String street, boolean isopen) {
  28.                 this.code = code;
  29.                 this.street = street;
  30.                 this.isopen = isopen;
  31.         }
  32. @Root
  33. public class Order {
  34.         @Attribute(required = true)
  35.         private Long id;
  36.         @Element(required = true)
  37.         private Date cdate;
  38.         @ElementList(required = false, type = Item.class)
  39.         private List<Item> itemList = new ArrayList<Item>(0);
  40.         public Order() {
  41.         }
  42.         public Order(Long id, Date cdate) {
  43.                 this.id = id;
  44.                 this.cdate = cdate;
  45.         }
  46. @Root
  47. public class Item {
  48.         @Attribute
  49.         private Long id;
  50.         @Element(required = true)
  51.         private int num;
  52.         @Element(required = false)
  53.         private BigDecimal price;
  54.         public Item() {
  55.         }
  56.         public Item(int num, Long id) {
  57.                 this.num = num;
  58.                 this.id = id;
  59.         }
  60. package test;
  61. import org.simpleframework.xml.Serializer;
  62. import org.simpleframework.xml.core.Persister;
  63. import java.util.Date;
  64. import java.io.File;
  65. /**
  66. * Created by IntelliJ IDEA.
  67. *
  68. * @author leizhimin 2008-12-29 22:05:16
  69. */
  70. public class TestSimple {
  71.         public static void main(String[] args) throws Exception {
  72.                 Customer customer = new Customer(111L, “张三”);
  73.                 Order order1 = new Order(1L, new Date());
  74.                 Order order2 = new Order(2L, new Date());
  75.                 Item item11 = new Item(1, 11L);
  76.                 Item item21 = new Item(2, 21L);
  77.                 Address address = new Address(“450000”, “瑞达路XX#”, true);
  78.                 customer.setAddress(address);
  79.                 customer.getOrderList().add(order1);
  80.                 customer.getOrderList().add(order2);
  81.                 order1.getItemList().add(item11);
  82.                 order2.getItemList().add(item21);
  83.                 Serializer serializer = new Persister();
  84.                 File result = new File(“outxml/customer.xml”);
  85.                 serializer.write(customer, result);
  86.                 Customer _obj = serializer.read(Customer.class, result);
  87.                 System.out.println(_obj.getName());
  88.                 System.out.println(_obj.getOrderList().get(0).getCdate());
  89.         }
  90. }

生成的文件:
<customer id=”111″>
<name>张三</name>
<address>
<code>450000</code>
<street>瑞达路XX#</street>
<isopen>true</isopen>
</address>
<orderList>
<order id=”1″>
<cdate>2008-12-29 23:36:36.390 CST</cdate>
<itemList>
<item id=”11″>
<num>1</num>
</item>
</itemList>
</order>
<order id=”2″>
<cdate>2008-12-29 23:36:36.390 CST</cdate>
<itemList>
<item id=”21″>
<num>2</num>
</item>
</itemList>
</order>
</orderList>
</customer>

控制台输出的内容:
张三
Mon Dec 29 23:36:36 CST 2008

Process finished with exit code 0

标签