当前位置:首页 > 开发 > 开源软件 > 正文

XStream-实现XML解析,生成与之对应的Java bean

发表于: 2013-07-06   作者:德安德鲁   来源:转载   浏览:
摘要: Xstream常用注解: @XStreamAlias("message") :   别名注解   作用目标: 类,字段   @XStreamImplicit 隐式集合  @XStreamImplicit(itemFieldName="subElementName") &

Xstream常用注解: 
@XStreamAlias("message") :

  别名注解  

作用目标: 类,字段  

@XStreamImplicit

隐式集合 

@XStreamImplicit(itemFieldName="subElementName")  
作用目标:

集合字段  

@XStreamConverter(SingleValueCalendarConverter.class)

注入转换器  
作用目标: 对象  

@XStreamAsAttribute 转换成属性  
作用目标: 字段  

@XStreamOmitField 忽略字段  
作用目标: 字段  

隐式集合: 
当我们使用集合类时不想显示集合,只显示里面的元素即可. 
使用隐式集合前: 
<list> 
 <element /> 
 <element /> 
<list> 

使用隐式集合后: 
 <element /> 
 <element /> 

解析XML生成java bean代码示例:

xml如下:

<?xml version="1.0" encoding="UTF-8"?>
<family>
	<phone>1234</phone>
	<address>shan'xi xi'an</address>
	<sports>football,basketball</sports>
	<members>
		<member role="father">
			<name>john</name>
			<age>42</age>
			<gender>man</gender>
		</member>
		<member role="mother">
			<name>lili</name>
			<age>40</age>
			<gender>women</gender>
		</member>
		<member role="son">
			<name>west</name>
			<age>5</age>
			<gender>man</gender>
		</member>
	</members>
</family>

 定义Family模型.

注:Family模型中sports属性为List<String>,即从xml读取的”football,basketball“要转化为List

所以自定义一个SportsConverter,并在sports属性上面增加注解

@XStreamConverter(SportsConverter.class)

/**
 * 
 */
package xStream;

import java.io.Serializable;
import java.util.List;

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

/**
 * @author Administrator
 * 
 */
@XStreamAlias("family")
public class Family implements Serializable
{
	/**
	 * 
	 */
	private static final long serialVersionUID = 2666558275262597117L;
	
	@XStreamAlias("phone")
	private String telephone;
	
	private List<Person> members;
	
	private String address;
	
	@XStreamConverter(SportsConverter.class)
	private List<String> sports;
	
	public String getTelephone()
	{
		return telephone;
	}
	
	public void setTelephone(String telephone)
	{
		this.telephone = telephone;
	}
	
	public List<Person> getMembers()
	{
		return members;
	}
	
	public void setMembers(List<Person> members)
	{
		this.members = members;
	}
	
	public String getAddress()
	{
		return address;
	}
	
	public void setAddress(String address)
	{
		this.address = address;
	}
	
	public List<String> getSports()
	{
		return sports;
	}
	
	public void setSports(List<String> sports)
	{
		this.sports = sports;
	}
	
	@Override
	public String toString()
	{
		return "Family [address=" + address + ", members=" + members
				+ ", sports=" + sports + ", telephone=" + telephone + "]";
	}
	
}

 

package xStream;

import java.io.Serializable;

import com.thoughtworks.xstream.annotations.XStreamAlias;
import com.thoughtworks.xstream.annotations.XStreamAsAttribute;
import com.thoughtworks.xstream.annotations.XStreamConverter;
import com.thoughtworks.xstream.converters.extended.ToStringConverter;

@XStreamAlias("member")
public class Person implements Serializable
{
	/**
	 * 
	 */
	private static final long serialVersionUID = -7139978490881468546L;
	
	@XStreamAsAttribute
	private String role;
	
	private String name;
	
	private String gender;
	
	private String age;
	
	public String getRole()
	{
		return role;
	}
	
	public void setRole(String role)
	{
		this.role = role;
	}
	
	public String getName()
	{
		return name;
	}
	
	public void setName(String name)
	{
		this.name = name;
	}
	
	public String getGender()
	{
		return gender;
	}
	
	public void setGender(String gender)
	{
		this.gender = gender;
	}
	
	public String getAge()
	{
		return age;
	}
	
	public void setAge(String age)
	{
		this.age = age;
	}
	
	@Override
	public String toString()
	{
		return "Person [age=" + age + ", gender=" + gender + ", name=" + name
				+ ", role=" + role + "]";
	}
	
}

 

自定义一个SportsConverter.java实现将String转化为List<String>

 

/**
 * 
 */
package xStream;

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

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;

/**
 * @author Administrator
 * 
 */
public class SportsConverter implements Converter
{
	
	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.thoughtworks.xstream.converters.Converter#marshal(java.lang.Object,
	 * com.thoughtworks.xstream.io.HierarchicalStreamWriter,
	 * com.thoughtworks.xstream.converters.MarshallingContext)
	 */
	@Override
	public void marshal(Object obj,
			HierarchicalStreamWriter hierarchicalstreamwriter,
			MarshallingContext marshallingcontext)
	{
		// TODO Auto-generated method stub
		
	}
	
	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.thoughtworks.xstream.converters.Converter#unmarshal(com.thoughtworks
	 * .xstream.io.HierarchicalStreamReader,
	 * com.thoughtworks.xstream.converters.UnmarshallingContext)
	 */
	@Override
	public Object unmarshal(HierarchicalStreamReader reader,
			UnmarshallingContext unmarshallingcontext)
	{
		List<String> sportsList = new ArrayList<String>();
		try
		{
			String sportCollect = reader.getValue();
			String[] sports = sportCollect.split(",");
			if ((null != sports) && (0 != sports.length))
			{
				for (String string : sports)
				{
					sportsList.add(string);
				}
				
			}
			
		}
		catch (Exception e)
		{
			e.printStackTrace();
		}
		return sportsList;
	}
	
	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.thoughtworks.xstream.converters.ConverterMatcher#canConvert(java.
	 * lang.Class)
	 */
	@Override
	public boolean canConvert(Class class1)
	{
		return class1.equals(ArrayList.class);
	}
}

 

/**
 * 
 */
package xStream;

import java.io.File;

import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.io.xml.Dom4JDriver;

/**
 * @author Administrator
 * 
 */
public class XStream2JaveBean
{
	
	/**
	 * @param args
	 */
	public static void main(String[] args)
	{
		XStream xStream = new XStream(new Dom4JDriver());
		xStream.processAnnotations(Family.class);
		xStream.processAnnotations(Person.class);
		
		Family family = (Family) xStream.fromXML(new File("d:\\NewFile.xml"));
		System.out.println(family);
	}
}

 执行XStream2JaveBean的main函数。结果如下:

Family [address=shan'xi xi'an, members=[Person [age=42, gender=man, name=john, role=father], Person [age=40, gender=women, name=lili, role=mother], Person [age=5, gender=man, name=west, role=son]], sports=[football, basketball], telephone=1234]
 

 成功获取到XML对应的Family java模型。

XStream-实现XML解析,生成与之对应的Java bean

  • 0

    开心

    开心

  • 0

    板砖

    板砖

  • 0

    感动

    感动

  • 0

    有用

    有用

  • 0

    疑问

    疑问

  • 0

    难过

    难过

  • 0

    无聊

    无聊

  • 0

    震惊

    震惊

推荐文章
编辑推荐
版权所有 IT知识库 CopyRight © 2009-2015 IT知识库 IT610.com , All Rights Reserved. 京ICP备09083238号