用Xstream 来解析xml文本

本文介绍如何利用Xstream库解析包含属性和值的XML文本,涉及toXml和toObj两种操作,通过注解实现转换。提供工具类、POJO类的简单示例以及测试类和测试结果。
摘要由CSDN通过智能技术生成

用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  
 * 2018717日
 */
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);
    }

    //xmljava对象
    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
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值