博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
利用XStream在Java对象和XML之间相互转换
阅读量:6625 次
发布时间:2019-06-25

本文共 5615 字,大约阅读时间需要 18 分钟。

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$[email]Itr@1cde100java.util.Abst[/email]ractList$Itr@1cde100} 



Process finished with exit code 0 

 
呵呵,XStream不错吧!
本文转自 leizhimin 51CTO博客,原文链接:http://blog.51cto.com/lavasoft/64114,如需转载请自行联系原作者
你可能感兴趣的文章
测不准原理主要指向微观
查看>>
排序算法java版,速度排行:冒泡排序、简单选择排序、直接插入排序、折半插入排序、希尔排序、堆排序、归并排序、快速排序...
查看>>
Android之ExpandableList扩展用法(基于BaseExpandableListAdapter)
查看>>
解决注册表映像劫持
查看>>
基于Redis架构的短信平台系统
查看>>
Java Daemon Control
查看>>
The Quick Guide to Python Eggs
查看>>
3D资源的后台加载
查看>>
怎样获取Web应用程序的路径
查看>>
xcode crash 查找 EXC_BAD_ACCESS 问题根源的方法
查看>>
PlotLegends 应用
查看>>
error: variable '__this_module' has initializer but incomplete type错误解决
查看>>
linux下为php添加mongodb扩展
查看>>
使用java.util.concurrent.ThreadFactory来创建线程
查看>>
zabbix 监控windows tcp 链接数
查看>>
Commonjs规范及Node模块实现
查看>>
Android——带文字阴影效果的TextView
查看>>
CentOS 7 使用 npm 失败 npm: symbol SSL_set_cert_cb
查看>>
基础知识|数据库的一些基本概念
查看>>
vue-resource promise兼容性问题
查看>>