溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

XStream如何實(shí)現(xiàn)Bean與xml互轉(zhuǎn)的案例

發(fā)布時(shí)間:2020-10-24 16:57:48 來源:億速云 閱讀:398 作者:小新 欄目:編程語言

這篇文章將為大家詳細(xì)講解有關(guān)XStream如何實(shí)現(xiàn)Bean與xml互轉(zhuǎn)的案例,小編覺得挺實(shí)用的,因此分享給大家做個(gè)參考,希望大家閱讀完這篇文章后可以有所收獲。

一、導(dǎo)入jar包

<dependency>
	<groupId>com.thoughtworks.xstream</groupId>
	<artifactId>xstream</artifactId>
	<version>1.4.8</version>
</dependency>

二、重要注解說明

@XStreamAlias 定義別名

@XStreamAsAttribute 定義為屬性

@XStreamOmitField  忽略

@XStreamConverter  處理日期格式

@XStreamImplicit(itemFieldName = "roles")  處理List

三、實(shí)例

1、定義JavaBean

import java.util.Date;
import java.util.List;

import com.thoughtworks.xstream.annotations.XStreamAlias;
import com.thoughtworks.xstream.annotations.XStreamAsAttribute;
import com.thoughtworks.xstream.annotations.XStreamConverter;
import com.thoughtworks.xstream.annotations.XStreamImplicit;
import com.thoughtworks.xstream.annotations.XStreamOmitField;
import com.tzz.util.xml.DateConverter;

@XStreamAlias("user")
public class User {

	@XStreamAsAttribute
	private Integer id;
	private String name;
	@XStreamOmitField
	private int age;
	private String sex;
	@XStreamConverter(value = DateConverter.class)
	private Date birthday = new Date();
	@XStreamImplicit(itemFieldName = "roles")
	private List<Role> roles;

	public Integer getId() {
		return id;
	}

	public void setId(Integer id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}

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

	public int getAge() {
		return age;
	}

	public void setAge(int age) {
		this.age = age;
	}

	public String getSex() {
		return sex;
	}

	public void setSex(String sex) {
		this.sex = sex;
	}

	public List<Role> getRoles() {
		return roles;
	}

	public void setRoles(List<Role> roles) {
		this.roles = roles;
	}

	public Date getBirthday() {
		return birthday;
	}

	public void setBirthday(Date birthday) {
		this.birthday = birthday;
	}
}
import com.thoughtworks.xstream.annotations.XStreamAlias;

@XStreamAlias("role")
public class Role {

	private Integer id;
	private String name;
	public Integer getId() {
		return id;
	}
	public void setId(Integer id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
}

2、轉(zhuǎn)換工具類

import java.io.Writer;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.dom4j.Element;

import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.core.util.QuickWriter;
import com.thoughtworks.xstream.io.HierarchicalStreamWriter;
import com.thoughtworks.xstream.io.xml.PrettyPrintWriter;
import com.thoughtworks.xstream.io.xml.XppDriver;

public class XStreamXmlUtil {

	/** XML轉(zhuǎn)Bean對象 */
	@SuppressWarnings("unchecked")
	public static <T> T xmlToBean(String xml, Class<T> clazz) {
		XStream xstream = new XStream();
		xstream.registerConverter(new DateConverter());
		xstream.autodetectAnnotations(true);
		xstream.processAnnotations(clazz);
		xstream.setClassLoader(clazz.getClassLoader());
		return (T) xstream.fromXML(xml);
	}

	/** Bean對象轉(zhuǎn)XML */
	public static String beanToXml(Object bean) {
//		return "<?xml version=\"1.0\" encoding=\"utf-8\"?>" + new XStream().toXML(bean);
		XStream xstream = new XStream();
		xstream.registerConverter(new DateConverter());
		xstream.autodetectAnnotations(true);
		return xstream.toXML(bean);
	}
}

3、測試類

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

import org.junit.Test;

import com.tzz.test.util.xml.entity.Role;
import com.tzz.test.util.xml.entity.User;
import com.tzz.util.xml.XStreamXmlUtil;

public class TestXStreamXmlUtil {

	@Test
	public void testBeanToXml() {
		try {
			User user = new User();
			user.setId(1);
			user.setName("Test");
			user.setAge(20);
			user.setSex("1");
			user.setBirthday(new Date());
			Role role = new Role();
			role.setId(1);
			role.setName("角色1");
			Role role2 = new Role();
			role2.setId(2);
			role2.setName("角色2");
			List<Role> roles = new ArrayList<Role>();
			roles.add(role);
			roles.add(role2);
			user.setRoles(roles);
			String xml = XStreamXmlUtil.beanToXml(user);
			System.out.println(xml);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	@Test
	public void testXmlToBean() {
		String xml = 
		  "<user id='1'>"+
			  "<name>Test</name>"+
			  "<sex>1</sex>"+
			  "<birthday>2016-03-10 16:12:46</birthday>"+
			  "<roles>"+
			    "<id>1</id>"+
			    "<name>角色1</name>"+
			  "</roles>"+
			  "<roles>"+
			    "<id>2</id>"+
			    "<name>角色2</name>"+
			  "</roles>"+
		   "</user>";
		User user = XStreamXmlUtil.xmlToBean(xml, User.class);
		System.out.println(user.getId() + "--" + user.getName());
		List<Role> roles = user.getRoles();
		for (Role r : roles) {
			System.out.println(r.getName());
		}
	}
}

4、測試結(jié)果

4.1、運(yùn)行testBeanToXml方法

<user id="1">
  <name>Test</name>
  <sex>1</sex>
  <birthday>2016-03-10 17:35:41</birthday>
  <roles>
    <id>1</id>
    <name>角色1</name>
  </roles>
  <roles>
    <id>2</id>
    <name>角色2</name>
  </roles>
</user>

4.2、運(yùn)行testXmlToBean方法

1--Test
角色1
角色2

關(guān)于XStream如何實(shí)現(xiàn)Bean與xml互轉(zhuǎn)的案例就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,可以學(xué)到更多知識。如果覺得文章不錯,可以把它分享出去讓更多的人看到。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI