simple-xml是一个xml和对象互转操作包,官方地址在这里:
http://simple.sourceforge.net/
上面提供了详细的说明和操作方法,非常强大。
这里转载下熔岩同学提供的例子,自己也从这里学到了很多,:
package test1;
import org.simpleframework.xml.*;
import org.simpleframework.xml.core.Persister;
import java.io.File;
import java.util.List;
import java.util.ArrayList;
@Root
public class Example {
@Element
private String text;
@Attribute
private int index;
@Element()
private boolean flag;
@Element(required = false)
private Integer num;
@ElementList(required = false)
private List<String> slist = new ArrayList<String>();
public Example() {
super();
}
public Example(String text, int index) {
this.text = text;
this.index = index;
// slist.add("sdf");
}
public String getMessage() {
return text;
}
public int getId() {
return index;
}
public static void main(String[] args) throws Exception {
Serializer serializer = new Persister();
Example example = new Example("Example message", 123);
File result = new File("outxml/example.xml");
serializer.write(example, result);
Example _obj = serializer.read(Example.class, result);
System.out.println(_obj.getMessage());
}
}
输入的文件内容:
<example index="123">
<text>Example message</text>
<flag>false</flag>
<slist class="java.util.ArrayList"/>
</example>
控制台输出信息:
Example message
Process finished with exit code 0
第二个例子:
@Root
public class Customer {
@Attribute(required = true)
private Long id;
@Element(required = true)
private String name;
@Element(required = false)
private Address address;
@ElementList(required = false, type = Order.class)
private List<Order> orderList = new ArrayList<Order>(0);
public Customer() {
}
public Customer(Long id, String name) {
this.id = id;
this.name = name;
}
@Root
public class Address {
@Element(required = true)
private String code;
@Element
private String street;
@Element
private boolean isopen;
public Address() {
}
public Address(String code, String street, boolean isopen) {
this.code = code;
this.street = street;
this.isopen = isopen;
}
@Root
public class Order {
@Attribute(required = true)
private Long id;
@Element(required = true)
private Date cdate;
@ElementList(required = false, type = Item.class)
private List<Item> itemList = new ArrayList<Item>(0);
public Order() {
}
public Order(Long id, Date cdate) {
this.id = id;
this.cdate = cdate;
}
@Root
public class Item {
@Attribute
private Long id;
@Element(required = true)
private int num;
@Element(required = false)
private BigDecimal price;
public Item() {
}
public Item(int num, Long id) {
this.num = num;
this.id = id;
}
package test;
import org.simpleframework.xml.Serializer;
import org.simpleframework.xml.core.Persister;
import java.util.Date;
import java.io.File;
/**
* Created by IntelliJ IDEA.
*
* @author leizhimin 2008-12-29 22:05:16
*/
public class TestSimple {
public static void main(String[] args) throws Exception {
Customer customer = new Customer(111L, "张三");
Order order1 = new Order(1L, new Date());
Order order2 = new Order(2L, new Date());
Item item11 = new Item(1, 11L);
Item item21 = new Item(2, 21L);
Address address = new Address("450000", "瑞达路XX#", true);
customer.setAddress(address);
customer.getOrderList().add(order1);
customer.getOrderList().add(order2);
order1.getItemList().add(item11);
order2.getItemList().add(item21);
Serializer serializer = new Persister();
File result = new File("outxml/customer.xml");
serializer.write(customer, result);
Customer _obj = serializer.read(Customer.class, result);
System.out.println(_obj.getName());
System.out.println(_obj.getOrderList().get(0).getCdate());
}
}
生成的文件:
<customer id="111">
<name>张三</name>
<address>
<code>450000</code>
<street>瑞达路XX#</street>
<isopen>true</isopen>
</address>
<orderList class="java.util.ArrayList">
<order id="1">
<cdate>2008-12-29 23:36:36.390 CST</cdate>
<itemList class="java.util.ArrayList">
<item id="11">
<num>1</num>
</item>
</itemList>
</order>
<order id="2">
<cdate>2008-12-29 23:36:36.390 CST</cdate>
<itemList class="java.util.ArrayList">
<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