用Xstream 来解析xml文本
文本内部还包含着既有属性又有值的这种情况 这边进行了toXml 和 toObj 的2种操作 目前我这边用的是注解来完成这一转换步骤
工具类
package com.xxx.common.util;
import com.xxx.xxx.base.vo.Data;
import com.xxx.xxx.base.vo.Field;
import com.xxx.xxx.base.vo.Service;
import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.io.xml.DomDriver;
/**
* XStream工具类
* @author Risk
* 2018 年7 月17 日
*/
public class XStreamUtil {
private static XStream xStream ;
static {
xStream = new XStream (new DomDriver() ) ;
xStream .processAnnotations (Service.class) ; //类加载 必须
xStream .processAnnotations (Data.class) ;
xStream .processAnnotations (Field.class) ;
}
//xml 转java 对象
public static Object xmlToBean (String xml) {
return xStream .fromXML (xml) ;
}
//java 对象转xml
public static String beanToXml (Object obj) {
return "<?xml version =\"1.0\" encoding =\"UTF -8\"?>\n " + xStream .toXML (obj) ;
}
}
pojo 简单的挑几个类来写
package com.xxx.xxx.base.vo;
import com.thoughtworks.xstream.annotations.XStreamAlias;
/**
* @author 作者 Risk;
* @version 创建时间:2018年7月17日 上午10:41:26
* 类说明: xml解析基础类
*/
@XStreamAlias("service") //该类是节点
public class Service {
@XStreamAlias ("sys-header" ) //该字段换成指定字段 节点
private SysHeader sysheader ;
@XStreamAlias ("app-header" )
private AppHeader appheader ;
@XStreamAlias ("local-header" )
private LocalHeader localheader ;
@XStreamAlias ("body" )
private Body body ;
public SysHeader getSysheader () {
return sysheader ;
}
public void setSysheader (SysHeader sysheader) {
this .sysheader = sysheader ;
}
public AppHeader getAppheader () {
return appheader ;
}
public void setAppheader (AppHeader appheader) {
this .appheader = appheader ;
}
public LocalHeader getLocalheader () {
return localheader ;
}
public void setLocalheader (LocalHeader localheader) {
this .localheader = localheader ;
}
public Body getBody () {
return body ;
}
public void setBody (Body body) {
this .body = body ;
}
}
package com.xxx.xxx.base.vo;
import com.thoughtworks.xstream.annotations.XStreamAlias;
import com.thoughtworks.xstream.annotations.XStreamAsAttribute;
import com.thoughtworks.xstream.annotations.XStreamConverter;
import com.thoughtworks.xstream.converters.extended.ToAttributedValueConverter;
/**
* @author 作者 Risk;
* @version 创建时间:2018年7月17日 上午10:41:26
* 类说明: xml解析基础类
*/
@XStreamAlias ("field" )
@XStreamConverter (value=ToAttributedValueConverter.class, strings={"text" })
public class Field {
@XStreamAsAttribute
private String length;
@XStreamAsAttribute
private String scale;
@XStreamAsAttribute
private String type;
private String text;
public String getText () {
return text;
}
public void setText (String text) {
this .text = text;
}
public String getLength () {
return length;
}
public void setLength (String length) {
this .length = length;
}
public String getScale () {
return scale;
}
public void setScale (String scale) {
this .scale = scale;
}
public String getType () {
return type;
}
public void setType (String type) {
this .type = type;
}
}
package com.xxx.xxx.base.vo;
import java.util.List;
import com.thoughtworks.xstream.annotations.XStreamAlias;
import com.thoughtworks.xstream.annotations.XStreamAsAttribute;
/**
* @author 作者 Risk;
* @version 创建时间:2018年7月17日 上午10:41:26
* 类说明: xml解析基础类
*/
@XStreamAlias ("data" )
public class Data {
private List<Data> struct;
private Field field;
private Data data;
@XStreamAsAttribute
private String name;
public String getName () {
return name;
}
public void setName (String name) {
this .name = name;
}
public Data getData () {
return data;
}
public void setData (Data data) {
this .data = data;
}
public Field getField () {
return field;
}
public void setField (Field field) {
this .field = field;
}
public List<Data> getStruct () {
return struct;
}
public void setStruct (List<Data> struct) {
this .struct = struct;
}
}
这里是测试类 上次忘传了 以及运行测试结果
package com .test .test
import java.util .ArrayList
import java.util .List
import com .cn .util .XStreamUtil
import pojo.Data
import pojo.Field
import pojo.Service
import pojo.SysHeader
public class TestXStream {
public static void main(String[] args) {
long time1 = System.currentTimeMillis ()
//测试java对象转xml,java对象中包含集合对象
Service service = new Service()
SysHeader sysHeader = new SysHeader()
Data data = new Data()
data.setName ("SYS_HEAD" )
ArrayList<Data> dataList = new ArrayList<>()
Data data2 = new Data()
data2.setName ("SERVICE_CODE" )
Field field = new Field()
field.setLength ("24" )
field.setScale ("0" )
field.setType ("string" )
field.setText ("MBSD_IFP_TP" )
data2.setField (field)
dataList.add (data2)
data.setStruct (dataList)
sysHeader.setData (data)
service.setSysheader (sysHeader)
String xml = XStreamUtil2.beanToXml (service)
System.out .println (xml)
//测试xml转java对象
Service service2 = (Service) XStreamUtil.xmlToBean (xml)
System.out .println (service2.getSysheader ().getData ().getName ())
List<Data> list = service2.getSysheader ().getData ().getStruct ()
for (Data e : list) {
System.out .println (e.getName ()+":" +e.getField ().getText ())
}
long time2 = System.currentTimeMillis ()
System.out .println ((time2-time1)+"ms" )
}
}
测试运行结果:
<?xml version="1.0" encoding="UTF-8"?>
<service >
<sys-header >
<data name ="SYS_HEAD" >
<struct >
<data name ="SERVICE_CODE" >
<field length ="24" scale ="0" type ="string" > MBSD_IFP_TP</field >
</data >
</struct >
</data >
</sys-header >
</service >
SYS_HEAD
SERVICE_CODE:MBSD_IFP_TP
370ms