XStream是一个能将java 对象与 xml字符串进行互相转换的这么一个java library。
官网地址:http://xstream.codehaus.org/
最新版本为1.4.2,下载地址:https://nexus.codehaus.org/content/repositories/releases/com/thoughtworks/xstream/xstream-distribution/1.4.2/xstream-distribution-1.4.2-bin.zip
本篇文章的最终目的是将如下XML与JavaBean互相转换(由于代码太长,有些地方我会写伪代码或者注释),比较复杂:
Subscribe.java
XML
对比上面的javabean与xml也能大致推断出,有一个Subscribe对应xml的根节点,而Subscribe类的属性则对应根节点下面的子节点。
但是需要注意的是,Params下面有2个同名子节点,并且这2个同名子节点都有属性,所以Params对应的应该为一个集合,集合中每一个元素为一个Param类的对象。
Param类又有2个属性,分别为Name和Value。
而XStream要实现对xml节点的属性与JavaBean之间进行转换,必须为该属性定义一个类,以及一个转换器,该转换器必须实现XStream中的某些接口,所以还需要5个类,分别为Param、Name、NameConverter、Value、ValueConverter:
Param.java
Name.java
NameConverter.java
Value.java
ValueConverter.java
Test.java
package com.xcy; import java.util.ArrayList; import com.thoughtworks.xstream.XStream; import com.thoughtworks.xstream.io.xml.DomDriver; /** * @author Siuon * CSDN:http://blog.csdn.net/xiaochunyong * */ public class Test { public static String object2XML(){ Subscribe subscribe = new Subscribe(); subscribe.setAccountNumber(1); subscribe.setAppCode("GMHL"); subscribe.setAppName("钢贸互联"); subscribe.setAppVersionName("1.0"); subscribe.setEndDate("2012/6/11 15:54:02"); subscribe.setOrderAppCode("ORDAPP07e47e28201206111554022999039"); subscribe.setOrderAppName("gmhl010的钢贸互联"); subscribe.setRoleLevel("0"); subscribe.setStartDate("2012/6/11 15:54:02"); subscribe.setStatus(0); subscribe.setStrucCode("CMPf6ec6a81201206111553460629752"); subscribe.setStrucName("gmhl010的公司"); subscribe.setUserCode("gmhl010"); subscribe.setUserId("1786"); subscribe.setUserName("gmhl010"); subscribe.setVersion("GMHL1.0"); ArrayList<Param> params = new ArrayList<Param>(); params.add(new Param(new Name("AuthStatus"), new Value("UnAuth"))); params.add(new Param(new Name("IsTradeUser"), new Value("False"))); subscribe.setParams(params); XStream xstream = new XStream(new DomDriver()); //如果不调用alias方法,进行别名,XStream默认是将包名+类名作为节点名称,也就是<com.xcy.Subscribe>.....</com.xcy.Subscribe> xstream.alias("Subscribe", Subscribe.class); xstream.alias("Param", Param.class); //节点中的属性对应java,除了创建类、转换器外,还必须在这里进行注册,缺一不可 xstream.useAttributeFor(Param.class, "Name"); xstream.useAttributeFor(Param.class, "Value"); xstream.registerConverter(new NameConverter()); xstream.registerConverter(new ValueConverter()); return xstream.toXML(subscribe); } public static Object xml2Object(String xml){ XStream xstream = new XStream(new DomDriver()); xstream.alias("Subscribe", Subscribe.class); xstream.alias("Param", Param.class); xstream.useAttributeFor(Param.class, "Name"); xstream.useAttributeFor(Param.class, "Value"); xstream.registerConverter(new NameConverter()); xstream.registerConverter(new ValueConverter()); return xstream.fromXML(xml); } public static void main(String[] args) { String xml = object2XML(); System.out.println("xml:\r\n"+xml); Subscribe subscribe = (Subscribe)xml2Object(xml); System.out.println("\r\n\r\n\r\nobject property:"); System.out.println(subscribe.getAppName()); System.out.println(subscribe.getParams().get(0).getName().getAttributeValue()); System.out.println(subscribe.getParams().get(0).getValue().getAttributeValue()); } }
由于比较复杂、不太好理解,所以笔者将源码打包上传了,不要积分。
有兴趣的可以去下载运行看看:XStream:Object与Xml字符串 互相转换