xstream实现java对象和XML的互相转换

一、XStream简介

 

1. XStream的功能:Java对象与XML之间进行互相转化。

 

2. XStream的特点:灵活易用、高性能、整洁的XML、转换策略定制、错误诊断等。

 

3. XStream使用场景:转换;持久化对象;配置;单元测试。

 

4. 依赖的包: xstream-[version].jar、xpp3-[version].jar、xmlpull-[version].jar

 

5. MAVEN中pom.xml文件配置

 

    com.thoughtworks.xstream

    xstream

    1.4.9

 

二、XStream的使用

 

1. 创建XStream实例

 

    XStream xstream = new XStream();

    XStream xstream = new XStream(new DomDriver());

    XStream xstream = new XStream(new StaxDriver());

 

2. 常用方法

 

    xstream.toXML():将对象转换成XML

    xstream.fromXML():将XML转换成对象

    xstream.alias():为指定类创建别名

    xstream.useAttributeFor():设置属性

    xstream.aliasField():为字段设置别名

    xstream.aliasAttribute():为属性设置别名

    xstream.addImplicitCollection():集合节点隐藏,只显示子信息节点

    xstream.processAnnotations():处理Annotations信息

    xstream.autodetectAnnotations():自动侦查注解

 

3. 常用的注解

 

    @XStreamAlias("message"):为类和字段设置别名

    @XStreamImplicit(itemFieldName="part"): 隐式集合注解

    @XStreamAsAttribute:转换成属性

    @XStreamOmitField:不跟XML对应,需要省略的字段

    @XStreamConverter():注入转换器

 

三、代码示例

 

1. 假设有如下XML内容要转换成JAVA对象

 


 

2. 创建对应的model类

 

 

import java.util.List;

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

@XStreamAlias("table")
public class Table 
{
	@XStreamAsAttribute
	private String name;
	
	@XStreamAsAttribute
	private String comment;
	
	@XStreamImplicit(itemFieldName="column")
	private List columnList;

	public String getName() 
	{
		return name;
	}

	public void setName(String name) 
	{
		this.name = name;
	}

	public String getComment() 
	{
		return comment;
	}

	public void setComment(String comment) 
	{
		this.comment = comment;
	}

	public List getColumnList() 
	{
		return columnList;
	}

	public void setColumnList(List columnList) 
	{
		this.columnList = columnList;
	}
}



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

@XStreamAlias("column")
public class Column 
{
	@XStreamAsAttribute
	private String name;
	
	@XStreamAsAttribute
	private String type;
	
	@XStreamAsAttribute
	private Integer length;
	
	@XStreamAsAttribute
	private String comment;

	public String getName() 
	{
		return name;
	}

	public void setName(String name) 
	{
		this.name = name;
	}

	public String getType() 
	{
		return type;
	}

	public void setType(String type) 
	{
		this.type = type;
	}

	public Integer getLength() 
	{
		return length;
	}

	public void setLength(Integer length) 
	{
		this.length = length;
	}

	public String getComment() 
	{
		return comment;
	}

	public void setComment(String comment) 
	{
		this.comment = comment;
	}
}

 

3. 转换代码

 

 

        XStream xstream = new XStream();
        xstream.processAnnotations(new Class[]{Table.class, Column.class});
        Table table = (Table)xstream.fromXML(xmlContent); // xmlContent为xml内容

 

 

你可能感兴趣的:(java)