Code example of XStream implementing Bean and xml mutual conversion

黄舟
Release: 2017-03-31 14:37:43
Original
2493 people have browsed it

1. Import jar package


	com.thoughtworks.xstream
	xstream
	1.4.8
Copy after login

2. Important annotations

@XStreamAlias ​​defines the alias

@XStreamAsAttribute is defined as attribute

@XStreamOmitField Ignore

@XStreamConverter Process date format

@XStreamImplicit(itemFieldName = "roles") ProcessList

3. Example

1. Define 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 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 getRoles() {
		return roles;
	}

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

	public Date getBirthday() {
		return birthday;
	}

	public void setBirthday(Date birthday) {
		this.birthday = birthday;
	}
}
Copy after login
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;
	}
}
Copy after login

2. Conversion tool class

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转Bean对象 */
	@SuppressWarnings("unchecked")
	public static  T xmlToBean(String xml, Class 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对象转XML */
	public static String beanToXml(Object bean) {
//		return "" + new XStream().toXML(bean);
		XStream xstream = new XStream();
		xstream.registerConverter(new DateConverter());
		xstream.autodetectAnnotations(true);
		return xstream.toXML(bean);
	}
}
Copy after login

3. Test class

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 roles = new ArrayList();
			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 = 
		  ""+
			  "Test"+
			  "1"+
			  "2016-03-10 16:12:46"+
			  ""+
			    "1"+
			    "角色1"+
			  ""+
			  ""+
			    "2"+
			    "角色2"+
			  ""+
		   "";
		User user = XStreamXmlUtil.xmlToBean(xml, User.class);
		System.out.println(user.getId() + "--" + user.getName());
		List roles = user.getRoles();
		for (Role r : roles) {
			System.out.println(r.getName());
		}
	}
}
Copy after login

4. Test results

4.1 , run testBeanToXml method


  Test
  1
  2016-03-10 17:35:41
  
    1
    角色1
  
  
    2
    角色2
  
Copy after login

4.2. Run testXmlToBean method

1--Test
角色1
角色2
Copy after login

The above is the detailed content of Code example of XStream implementing Bean and xml mutual conversion. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!