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

效果图如下:

xstream使用注解实现xml和对象转化_第1张图片

实现方式:

根节点:

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节点:

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 = new ArrayList();

        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 value = new ArrayList();

        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);,则会爆出:

xstream使用注解实现xml和对象转化_第2张图片

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

xstream使用注解实现xml和对象转化_第3张图片

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

xstream使用注解实现xml和对象转化_第4张图片

你可能感兴趣的:(java)