欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

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

程序员文章站 2022-04-12 20:53:53
...

一、XStream简介

 

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

 

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

 

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

 

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

 

5. MAVEN中pom.xml文件配置

 

<dependency>

    <groupId>com.thoughtworks.xstream</groupId>

    <artifactId>xstream</artifactId>

    <version>1.4.9</version>

</dependency>

 

二、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对象

 

<?xml version="1.0" encoding="UTF-8"?>
<table name="ts_user" comment="用户信息">
	<column name="id" type="char" length="10" comment="ID"></column>
	<column name="name" type="varchar" length="50" comment="用户名"></column>
	<column name="password" type="varchar" length="100" comment="密码"></column>
	<column name="age" type="int" length="11" comment="年龄"></column>
	<column name="createDate" type="date" comment="创建日期"></column>
</table>

 

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<Column> 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<Column> getColumnList() 
	{
		return columnList;
	}

	public void setColumnList(List<Column> 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内容