使用XStream将Java对象和XML之间相互转换

xStream可以轻易的将Java对象和xml文档相互转换,而且可以修改某个特定的属性和节点名称,而且也支持json的转换;

xStream.autodetectAnnotations(true);//设置自动扫描注解

使用jar文件:

XmlUtil.java

package com.ifly.demo.utils;

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

public class XmlUtil {

	// object--->xml
	public static String simpleobject2xml(Object obj) {
		XStream xStream = new XStream(new DomDriver());
		xStream.alias(obj.getClass().getSimpleName(), obj.getClass());
		String xml = xStream.toXML(obj);
		return xml;
	}

	// xml--->object
	public static Object simplexml2object(String xml, Object obj) {
		XStream xStream = new XStream(new DomDriver());
		xStream.alias(obj.getClass().getSimpleName(), obj.getClass());
		Object reobj = xStream.fromXML(xml);
		return reobj;
	}
}

XmlUtilTest.java

package com.ifly.demo.test;

import java.util.Date;

import org.junit.Test;

import com.ifly.demo.domain.Person;
import com.ifly.demo.utils.XmlUtil;

public class XmlUtilTest {

	@Test
	public void testSimpleObject2xml() {
		Person person = new Person();
		person.setId(1);
		person.setName("sillycat");
		person.setEmail("test@test.com");
		person.setTime(new Date());
		String xml = XmlUtil.simpleobject2xml(person);
		System.out.println(xml);
	}

	@Test
	public void testSimpleXml2object() {
		Person person = new Person();
		person.setId(1);
		person.setName("sillycat");
		person.setEmail("test@test.com");
		person.setTime(new Date());

		String xml = XmlUtil.simpleobject2xml(person);

		Person temp = (Person) XmlUtil.simplexml2object(xml, new Person());
		System.out.println(temp);
		System.out.println(temp.getName());
		System.out.println(temp.getEmail());
		System.out.println(temp.getTime());
	}

}

testSimpleObject2xml()
结果:

<Person>
  <id>1</id>
  <name>sillycat</name>
  <email>test@test.com</email>
  <time>2014-12-10 13:57:00.259 UTC</time>
</Person>
testSimpleXml2object() 
结果:

com.ifly.demo.domain.Person@13cd5b5
sillycat
test@test.com
Wed Dec 10 21:58:56 CST 2014

实体类:Person.java

package com.ifly.demo.domain;

import java.util.Date;

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

public class Person {
	/*
	 * 变为属性
	 */
	@XStreamAsAttribute
	private int id;
	/*
	 * 忽略
	 */
	@XStreamOmitField
	private String name;
	private String email;
	private Date time;

	public int getId() {
		return id;
	}

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

	public String getEmail() {
		return email;
	}

	public void setEmail(String email) {
		this.email = email;
	}

	public String getName() {
		return name;
	}

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

	public Date getTime() {
		return time;
	}

	public void setTime(Date time) {
		this.time = time;
	}

}




  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

时间辜负了谁

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值