xstream使用注解实现xml和对象转化

效果图如下:

实现方式:

根节点:

import java.util.ArrayList;
import java.util.List;

import com.thoughtworks.xstream.annotations.XStreamAlias;
import com.thoughtworks.xstream.annotations.XStreamImplicit;

@XStreamAlias("LM_LOV_CONFIG")
public class LM_LOV_CONFIG {
	//多个节点使用:@XStreamImplicit(itemFieldName = "")
	@XStreamImplicit(itemFieldName = "TIME")
	private List<Time> times = new ArrayList<Time>();
	@XStreamAlias("SCREENING")
	private Screening screening;

        get...
        set...
}

对于TIME节点:

import com.thoughtworks.xstream.annotations.XStreamAlias;
import com.thoughtworks.xstream.annotations.XStreamConverter;

@XStreamAlias("TIME")
//多加一个@XStreamConverter指定到TimeConverter类
@XStreamConverter(TimeConverter.class)
public class Time {
	//不需要加注解
	private String property;
	
	private String value;

        get...
        set...
}
import com.thoughtworks.xstream.converters.Converter;
import com.thoughtworks.xstream.converters.MarshallingContext;
import com.thoughtworks.xstream.converters.UnmarshallingContext;
import com.thoughtworks.xstream.io.HierarchicalStreamReader;
import com.thoughtworks.xstream.io.HierarchicalStreamWriter;

public class TimeConverter implements Converter {

	@Override
	public boolean canConvert(Class arg0) {
		return arg0.equals(Time.class);
	}

	@Override
	public void marshal(Object arg0, HierarchicalStreamWriter arg1,
			MarshallingContext arg2) {
		Time time = (Time) arg0;
                //下面两行顺序不能变
		arg1.addAttribute("type", time.getProperty());
		arg1.setValue(time.getValue());
		
	}

	@Override
	public Object unmarshal(HierarchicalStreamReader arg0,
			UnmarshallingContext arg1) {
		Time time = new Time();
		time.setProperty(arg0.getAttribute("type"));
		time.setValue(arg0.getValue());
		return time;
	}

}

SCREENING节点:

import java.util.ArrayList;
import java.util.List;

import com.thoughtworks.xstream.annotations.XStreamAlias;
import com.thoughtworks.xstream.annotations.XStreamImplicit;

@XStreamAlias("SCREENING")
public class Screening {
	@XStreamImplicit(itemFieldName = "info")
	private List<Info> info = new ArrayList<Info>();

        get...
        set...
	
}

info节点

import java.util.ArrayList;
import java.util.List;

import com.thoughtworks.xstream.annotations.XStreamAlias;
import com.thoughtworks.xstream.annotations.XStreamAsAttribute;
import com.thoughtworks.xstream.annotations.XStreamImplicit;

@XStreamAlias("info")
public class Info {
        //添加节点属性name
	@XStreamAsAttribute
        private String name;

	@XStreamAlias("key")
	private String key;

	@XStreamAlias("type")
	private String type;

	@XStreamImplicit(itemFieldName = "value")
	private List<Values> value = new ArrayList<Values>();

        get...
        set...
}

value节点和TIME差不多,只是缺少一个属性

import com.thoughtworks.xstream.annotations.XStreamAlias;
import com.thoughtworks.xstream.annotations.XStreamConverter;

@XStreamAlias("value")
@XStreamConverter(ValueConverter.class)
public class Values {
	
	private String v;

	public String getV() {
		return v;
	}

	public void setV(String v) {
		this.v = v;
	}

}
import com.thoughtworks.xstream.converters.Converter;
import com.thoughtworks.xstream.converters.MarshallingContext;
import com.thoughtworks.xstream.converters.UnmarshallingContext;
import com.thoughtworks.xstream.io.HierarchicalStreamReader;
import com.thoughtworks.xstream.io.HierarchicalStreamWriter;

public class ValueConverter implements Converter{

	@Override
	public boolean canConvert(Class arg0) {
		return arg0.equals(Values.class);
	}

	@Override
	public void marshal(Object arg0, HierarchicalStreamWriter arg1,
			MarshallingContext arg2) {
		Values value = (Values) arg0;
		arg1.setValue(value.getV());
	}

	@Override
	public Object unmarshal(HierarchicalStreamReader arg0,
			UnmarshallingContext arg1) {
		Values value = new Values();
		value.setV(arg0.getValue());
		return value;
	}

}

测试:

import java.io.File;

import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.io.xml.DomDriver;
import com.thoughtworks.xstream.io.xml.XmlFriendlyNameCoder;

public class test {

	public static void main(String[] args) {
	    //1.XmlFriendlyNameCoder当类中名称有单下划线的时候,
            //转成XML时候会变成双下划线,如LM_LOV_CONFIG,则转换成XML标签时候则为LM__LOV__CONFIG

            //解决单下划线变双下划线的方法就是使用XmlFriendlyNameCoder,如XmlFriendlyNameCoder("-_", "_")
	    XStream xstream = new XStream(new DomDriver("UTF-8",new XmlFriendlyNameCoder("-_","_")));
	    xstream.autodetectAnnotations(true);
	    XStream.setupDefaultSecurity(xstream);
	    xstream.allowTypes(new Class[]{LM_LOV_CONFIG.class,Info.class});
	    xstream.processAnnotations(LM_LOV_CONFIG.class);
	    LM_LOV_CONFIG CONFIG = (LM_LOV_CONFIG) xstream.fromXML(new File("C:\\Users\\aa\\Desktop\\测试\\DA_LOV_Config.xml"));
	    String resultXml = xstream.toXML(CONFIG);
	    System.out.printf("=======================\n" + resultXml);
	}

}

注:

1.如果注释掉XStream.setupDefaultSecurity(xstream);,则会爆出:

2.在XStream.setupDefaultSecurity(xstream);存在的情况下,如果注释xstream.allowTypes(new Class[]{LM_LOV_CONFIG.class});则会爆出:

 3.如果注释掉xstream.processAnnotations(LM_LOV_CONFIG.class);,则会爆出

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值