对象(java bean)与xml之间的转换(Xstream)

一、前言

        之前在做一个微信公众号的项目,项目中有用到一种技术,将调用接口后返回的xml字符串转换为对象(java bean),个人觉得这种方式还是蛮好用的,所以在这里分享下。

二、引入jar包

我是采用的maven项目,所以直接引入依赖就可以了:


    com.thoughtworks.xstream
    xstream
    1.4.10

三、案列分享

1.非注解方式实现对象(java bean)与xml互转

1.1 创建两个实体类

public class Bank {
	
	private String name;
	
	private String address;
	
	private List accounts;

	public String getName() {
		return name;
	}

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

	public String getAddress() {
		return address;
	}

	public void setAddress(String address) {
		this.address = address;
	}

	public List getAccounts() {
		return accounts;
	}

	public void setAccounts(List accounts) {
		this.accounts = accounts;
	}

	@Override
	public String toString() {
		return "Bank [name=" + name + ", address=" + address + ", accounts=" + accounts + "]";
	}
}
public class Account {
	
	private int accoutId;
	private String accountName;
	private double accountMoeny;
	private String telNum;
	public int getAccoutId() {
		return accoutId;
	}
	public void setAccoutId(int accoutId) {
		this.accoutId = accoutId;
	}
	public String getAccountName() {
		return accountName;
	}
	public void setAccountName(String accountName) {
		this.accountName = accountName;
	}
	public double getAccountMoeny() {
		return accountMoeny;
	}
	public void setAccountMoeny(double accountMoeny) {
		this.accountMoeny = accountMoeny;
	}
	public String getTelNum() {
		return telNum;
	}
	public void setTelNum(String telNum) {
		this.telNum = telNum;
	}
	
	@Override
	public String toString() {
		return "Account [accoutId=" + accoutId + ", accountName=" + accountName + ", accountMoeny=" + accountMoeny
				+ ", telNum=" + telNum + "]";
	}
}

1.2 创建一个测试类

public class XstreamTest {
	
	public static void main(String[] args) {
		//采用非注解方式将对象转xml
		String resultXml = beanToXml();
		System.out.println(resultXml);
		System.out.println("-------------------------------------------------------------------");
		Bank bank = xmlToBean(resultXml);
		System.out.println(bank);
		System.out.println("*******************************************************************");
	}

	private static Bank xmlToBean(String resultXml) {
		//创建xstream对象
		XStream xStream = new XStream();
		//将别名与xml名字对应
		xStream.alias("bank",Bank.class);
		xStream.alias("account", Account.class);
		//将字符串类型的xml转换为对象
		Bank bank = (Bank)xStream.fromXML(resultXml);
		return bank;
	}

	private static String beanToXml() {
		Bank bank = new Bank();
		Account account1 = new Account();
		Account account2 = new Account();
		account1.setAccoutId(1);
		account1.setAccountName("dengchao");
		account1.setAccountMoeny(100);
		account1.setTelNum("1333333333");
		
		account2.setAccoutId(2);
		account2.setAccountName("sunli");
		account2.setAccountMoeny(200);
		account2.setTelNum("14444444");
		
		List accounts = new ArrayList();
		accounts.add(account1);
		accounts.add(account2);
		
		bank.setName("中国人民银行");
		bank.setAddress("地球");
		bank.setAccounts(accounts);
		//创建xstream对象
		XStream xStream = new XStream();
		//给指定类起别名
		xStream.alias("bank", Bank.class);
		xStream.alias("account", Account.class);
		//将对象转换成xml字符串
		String xml = xStream.toXML(bank);
		
		return xml;
	}
}

2.注解方式实现对象(java bean)与xml互转

2.1创建实体类

实体还是跟之前的一样,区别在于在实体类的上面加上@XStreamAlias("account")和@XStreamAlias("bank")

对象(java bean)与xml之间的转换(Xstream)_第1张图片

对象(java bean)与xml之间的转换(Xstream)_第2张图片

2.2 创建一个测试类

public class XstreamTest {
	
	public static void main(String[] args) {
		//采用注解方式将xml转对象
		String resultXml2 = beanToXmlByAnno();
		System.out.println(resultXml2);
		System.out.println("-------------------------------------------------------------------");
		//xml转对象
		Bank bank2 = xmlToBeanByAnno(resultXml2);
		System.out.println(bank2);
		
	}
	private static String beanToXmlByAnno() {
		Bank bank = new Bank();
		Account account1 = new Account();
		Account account2 = new Account();
		account1.setAccoutId(1);
		account1.setAccountName("dengchao");
		account1.setAccountMoeny(100);
		account1.setTelNum("1333333333");
		
		account2.setAccoutId(2);
		account2.setAccountName("sunli");
		account2.setAccountMoeny(200);
		account2.setTelNum("14444444");
		
		List accounts = new ArrayList();
		accounts.add(account1);
		accounts.add(account2);
		
		bank.setName("中国人民银行");
		bank.setAddress("地球");
		bank.setAccounts(accounts);
		//创建xstream对象
		XStream xStream = new XStream();
		//使用注解修改对象名称
	    xStream.processAnnotations(new Class[]{Bank.class,Account.class});
		String xml = xStream.toXML(bank);
		
		return xml;
	}
	
	private static Bank xmlToBeanByAnno(String resultXml) {
		//创建xstream对象
		XStream xStream = new XStream();
		//使用注解修改对象名称
	        xStream.processAnnotations(new Class[]{Bank.class,Account.class});
		//将字符串类型的xml转换为对象
		Bank bank = (Bank)xStream.fromXML(resultXml);
		return bank;
	}

}

三、区别

注解方式与非注解方式的区别在于给类起别名的方式不同,非注解方式是直接调用Xstream自带alias方式起别名,如下:

对象(java bean)与xml之间的转换(Xstream)_第3张图片

注解方式则是在需要在类的顶部添加@XStreamAlias注释然后再加上一个别名,在进行处理时需要调用processAnnotations方法,class数组中设计到是xml数据设计到相关类的class数组,如下:

对象(java bean)与xml之间的转换(Xstream)_第4张图片


对象(java bean)与xml之间的转换(Xstream)_第5张图片

你可能感兴趣的:(java)