随笔-6  评论-1  文章-0  trackbacks-0
XStream 是一个轻量级的、简单易用的开放源代码Java™库,用于将 Java 对象序列化为 XML 或者再转换回来

XStream 对象相当Java对象和XML之间的转换器,转换过程是双向的。创建XSteam对象的方式很简单,只需要new XStream()即可。
Java 到xml,用toXML()方法。
Xml到Java,用fromXML()方法。

在没有任何设置默认情况下,java到xml的映射,是java成员名对应xml的元素名,java类的全名对应xml根元素的名字。而实际中,往往是xml和java类都有了,要完成相互转换,必须进行别名映射。
 
XStream 的编码:
本身并没有实现编码,必须依靠java.io.Writer来实现输出编码的转换。

XStream 的常用方法
xstream.omitField(mytest.class,"name");定义某一个属性的值不进行xml序列化。
xstream.alias("cat", Cat.class); 对某一个类进行别名定义
xstream.aliasField("age",Cat.class, "mAge");对某一个类的属性进行别名定义。
xstream.useAttributeFor(String.class);对所有String类型的字段定义为属性tag显示
xstream.useAttributeFor("name".String.class);对所有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();
         //对某一个类进行别名定义
         xstream.alias("地址",Address.class);
         xstream.alias("联系人",Person.class);
         //对某一个类的属性进行别名定义
         xstream.aliasField("编号",Person.class, "id");
         xstream.aliasField("姓名",Person.class, "name");
         xstream.aliasField("年龄",Person.class, "age");
         xstream.aliasField("联系方式",Person.class, "addresses");
         xstream.aliasField("编号",Address.class, "id");
         xstream.aliasField("名称",Address.class, "addressName");
         xstream.aliasField("生日",Person.class, "birthday");
         //定义某一个属性的值不进行xml序列化。
         xstream.omitField(Person.class,"date");
         //对id字段定义为属性显示
         xstream.useAttributeFor(Person.class,"id");
         xstream.useAttributeFor(Address.class,"id");
         //对所有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 阅读(1772) 评论(1)  编辑  收藏 所属分类: xml

评论:
# re: java对象与xml文件转换[未登录] 2015-09-21 22:36 | ccc
bvc  回复  更多评论
  

只有注册用户登录后才能发表评论。


网站导航: