利用XStream在Java对象和XML之间相互转换

XStream是个好东西,可以方便在Java对象和XML之间相互转换,效率和可读性都非常高,下面是我写的一个测试的例子,放出来一块看看。
 
需要的jar包如下:
xpp3_min-1.1.4c.jar
xstream-1.3.jar
 
目标:将Persons对象转换xml,再将xml转化为Persons对象。
 
实现源码列表:
 
package com.lavasoft.demo3;

import com.thoughtworks.xstream.XStream;

import java.util.List;
import java.util.ArrayList;
import java.util.Iterator;
import java.io.StringReader;
import java.io.FileReader;
import java.io.File;
import java.io.FileNotFoundException;

import org.dom4j.io.SAXReader;
import org.dom4j.Document;
import org.dom4j.Node;
import org.dom4j.DocumentException;

/**
* Created by IntelliJ IDEA.
* File: demo3.java
* User: leizhimin
* Date: 2008-3-4 14:44:03
*/

public class XStreamDemo {
     public static void main(String args[]) {
        testBean2XML();
    }

     /**
     * 生成一个Persons对象
     *
     * @return Persons对象
     */

     public static Persons getPersons() {
        Address add1 = new Address( "type1", "郑州市经三路财富广场1");
        Address add2 = new Address( "type2", "郑州市经三路财富广场2");
        List<Address> addlist1 = new ArrayList<Address>();
        addlist1.add(add1);
        addlist1.add(add2);

        Address add3 = new Address( "type3", "郑州市经三路财富广场3");
        Address add4 = new Address( "type4", "郑州市经三路财富广场4");
        List<Address> addlist2 = new ArrayList<Address>();
        addlist2.add(add3);
        addlist2.add(add4);

        Addresses addes1 = new Addresses(addlist1);
        Addresses addes2 = new Addresses(addlist2);
        Person person1 = new Person(addes1, "6666554", "lavasoft", "man");
        Person person2 = new Person(addes2, "7777754", "yutian", "man");

        List<Person> listPerson = new ArrayList<Person>();
        listPerson.add(person1);
        listPerson.add(person2);
        Persons persons = new Persons(listPerson, "001");
         return persons;
    }

     /**
     * 利用XStream在Java对象和XML之间相互转换
     */

     public static void testBean2XML() {
        System.out.println( "将Java对象转换为xml!\n");
        Persons persons = getPersons();
        XStream xstream = new XStream();
        xstream.alias( "address", Address. class);
        xstream.alias( "addresses", Addresses. class);
        xstream.alias( "person", Person. class);
        xstream.alias( "persons", Persons. class);
        String xml = xstream.toXML(persons);
        System.out.println(xml);

        System.out.println( "\n将xml转换为Java对象!");
        Persons cre_person = (Persons) xstream.fromXML(xml);
        System.out.println(cre_person.toString());
    }
}
 
public class Persons {
     private String type;
     private List<Person> listPerson;

     public Persons(List<Person> listPerson, String type) {
         this.listPerson = listPerson;
         this.type = type;
    }

     public String getType() {
         return type;
    }

     public void setType(String type) {
         this.type = type;
    }

     public List<Person> getListPerson() {
         return listPerson;
    }

     public void setListPerson(List<Person> listPerson) {
         this.listPerson = listPerson;
    }

     public String toString() {
        StringBuffer sb= new StringBuffer();
         for(Iterator it=listPerson.iterator();it.hasNext();){
            Person p=(Person)it.next();
            sb.append(it.toString());
        }
         return "Persons{" +
                 "type='" + type + '\'' +
                 ", listPerson=" + sb.toString() +
                 "}\n";
    }
}
 
public class Person {
     private String name;
     private String sex;
     private String tel;
     private Addresses addes;

     public Person(Addresses addes, String name, String sex, String tel) {
         this.addes = addes;
         this.name = name;
         this.sex = sex;
         this.tel = tel;
    }

     public Addresses getAddes() {
         return addes;
    }

     public void setAddes(Addresses addes) {
         this.addes = addes;
    }

     public String getName() {
         return name;
    }

     public void setName(String name) {
         this.name = name;
    }

     public String getSex() {
         return sex;
    }

     public void setSex(String sex) {
         this.sex = sex;
    }

     public String getTel() {
         return tel;
    }

     public void setTel(String tel) {
         this.tel = tel;
    }

     public String toString() {
         return "Person{" +
                 "addes=" + addes.toString() +
                 ", name='" + name + '\'' +
                 ", sex='" + sex + '\'' +
                 ", tel='" + tel + '\'' +
                 "}\n";
    }
}
 
public class Addresses {
     private List<Address> listAdd;

     public Addresses(List<Address> listAdd) {
         this.listAdd = listAdd;
    }

     public List<Address> getListAdd() {
         return listAdd;
    }

     public void setListAdd(List<Address> listAdd) {
         this.listAdd = listAdd;
    }

     public String toString() {
        StringBuffer sb= new StringBuffer();
         for(Iterator it=listAdd.iterator();it.hasNext();){
            Address add=(Address)it.next();
            sb.append(add.toString());
        }
         return "Addresses{" +
                 "listAdd=" + sb.toString() +
                 "}\n";
    }
}
 
public class Address {
     private String addType;
     private String place;

     public Address(String addType, String place) {
         this.addType = addType;
         this.place = place;
    }

     public String getAddType() {
         return addType;
    }

     public void setAddType(String addType) {
         this.addType = addType;
    }

     public String getPlace() {
         return place;
    }

     public void setPlace(String place) {
         this.place = place;
    }


     public String toString() {
         return "Address{" +
                 "addType='" + addType + '\'' +
                 ", place='" + place + '\'' +
                 "}\n";
    }
}
 
运行测试类XStreamDemo,打印出结果:
 
将Java对象转换为xml!

< persons >
   < type >001 </ type >
   < listPerson >
     < person >
       < name >6666554 </ name >
       < sex >lavasoft </ sex >
       < tel >man </ tel >
       < addes >
         < listAdd >
           < address >
             < addType >type1 </ addType >
             < place >郑州市经三路财富广场1 </ place >
           </ address >
           < address >
             < addType >type2 </ addType >
             < place >郑州市经三路财富广场2 </ place >
           </ address >
         </ listAdd >
       </ addes >
     </ person >
     < person >
       < name >7777754 </ name >
       < sex >yutian </ sex >
       < tel >man </ tel >
       < addes >
         < listAdd >
           < address >
             < addType >type3 </ addType >
             < place >郑州市经三路财富广场3 </ place >
           </ address >
           < address >
             < addType >type4 </ addType >
             < place >郑州市经三路财富广场4 </ place >
           </ address >
         </ listAdd >
       </ addes >
     </ person >
   </ listPerson >
</ persons >

将xml转换为Java对象!
Persons{type='001', listPerson=java.util.AbstractList$ Itr@1cde100java.util.AbstractList$Itr@1cde100}


Process finished with exit code 0


  转载请标明出处 http://blog.csdn.net/shimiso 

技术交流群:66756039


 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值