XStream 是一個(gè)輕量級(jí)的、簡(jiǎn)單易用的開放源代碼Java™庫(kù),用于將 Java 對(duì)象序列化為 XML 或者再轉(zhuǎn)換回來
XStream 對(duì)象相當(dāng)Java對(duì)象和XML之間的轉(zhuǎn)換器,轉(zhuǎn)換過程是雙向的。創(chuàng)建XSteam對(duì)象的方式很簡(jiǎn)單,只需要new XStream()即可。Java 到xml,用toXML()方法。Xml到Java,用fromXML()方法。在沒有任何設(shè)置默認(rèn)情況下,java到xml的映射,是java成員名對(duì)應(yīng)xml的元素名,java類的全名對(duì)應(yīng)xml根元素的名字。而實(shí)際中,往往是xml和java類都有了,要完成相互轉(zhuǎn)換,必須進(jìn)行別名映射。 XStream 的編碼:本身并沒有實(shí)現(xiàn)編碼,必須依靠java.io.Writer來實(shí)現(xiàn)輸出編碼的轉(zhuǎn)換。XStream 的常用方法xstream.omitField(mytest.class,"name");定義某一個(gè)屬性的值不進(jìn)行xml序列化。xstream.alias("cat", Cat.class); 對(duì)某一個(gè)類進(jìn)行別名定義xstream.aliasField("age",Cat.class, "mAge");對(duì)某一個(gè)類的屬性進(jìn)行別名定義。xstream.useAttributeFor(String.class);對(duì)所有String類型的字段定義為屬性tag顯示xstream.useAttributeFor("name".String.class);對(duì)所有String類型的字段名成為name 定義為屬性tag顯示package com.ljh.bean;public class Address { private int id; private String addressName; public Address(){} public Address(int id, String addressName) { this.id= id; this.addressName= addressName; } public int getId() { return id; } public void setId(int id) { this.id= id; } public String getAddressName() { return addressName; } public void setAddressName(String addressName) { this.addressName= addressName; }}package com.ljh.bean;import java.util.ArrayList;import java.util.Date;import java.util.List;public class Person { private int id; private String name; private int age; private String birthday; private List<Address> addresses = new ArrayList<Address>(); private Date date = new Date(); public Person(int id, String name, int age,String birthday) { this.id= id; this.name= name; this.age= age; this.birthday= birthday; } public List<Address> getAddresses() { return addresses; } public void setAddresses(List<Address> addresses) { this.addresses= addresses; } public Date getDate() { return date; } public void setDate(Date date) { this.date= date; } public String getBirthday() { return birthday; } public void setBirthday(String birthday) { this.birthday= birthday; } public int getId() { return id; } public void setId(int id) { this.id= id; } public String getName() { return name; } public void setName(String name) { this.name= name; } public int getAge() { return age; } public void setAge(int age) { this.age= age; } public void add(Address address){ addresses.add(address); }}package com.ljh.xstream;import java.io.PrintWriter;import com.ljh.bean.Address;import com.ljh.bean.Person;import com.thoughtworks.xstream.XStream;public class XStreamTest { public static void main(String[] args) throws Exception { Address address1 = new Address(1,"北京"); Address address2 = new Address(2,"天津"); Address address3 = new Address(3,"上海"); Person p = new Person(1,"ljh",38,"2111-11-11 11:11"); p.add(address1); p.add(address2); p.add(address3); XStream xstream = new XStream(); //對(duì)某一個(gè)類進(jìn)行別名定義 xstream.alias("地址",Address.class); xstream.alias("聯(lián)系人",Person.class); //對(duì)某一個(gè)類的屬性進(jìn)行別名定義 xstream.aliasField("編號(hào)",Person.class, "id"); xstream.aliasField("姓名",Person.class, "name"); xstream.aliasField("年齡",Person.class, "age"); xstream.aliasField("聯(lián)系方式",Person.class, "addresses"); xstream.aliasField("編號(hào)",Address.class, "id"); xstream.aliasField("名稱",Address.class, "addressName"); xstream.aliasField("生日",Person.class, "birthday"); //定義某一個(gè)屬性的值不進(jìn)行xml序列化。 xstream.omitField(Person.class,"date"); //對(duì)id字段定義為屬性顯示 xstream.useAttributeFor(Person.class,"id"); xstream.useAttributeFor(Address.class,"id"); //對(duì)所有String類型的字段名成為name 定義為屬性tag顯示 //xstream.useAttributeFor("name".String.class); xstream.toXML(p,new PrintWriter("c:\\person.xml","utf-8")); }}
posted on 2012-03-31 11:01
gzakoa 閱讀(1777)
評(píng)論(1) 編輯 收藏 所屬分類:
xml