Detailed introduction to JavaBean and XML mutual conversion tool class

黄舟
Release: 2017-04-01 13:16:40
Original
1899 people have browsed it

Use the jar package of XStream
x-stream.github.io/index.html
See the attachment for the jar package
XStream is a simple library to serialize objects to XML and back again.
Entity class

public class Person {
	 
	 private String firstname;
	 private String lastname;
	 private PhoneNumber phone;
	 private PhoneNumber fax;
	 
	 public Person(String firstname,String lastname){
		 this.firstname = firstname; 
		 this.lastname = lastname; 
	 }
	
	 public String getFirstname() {
		return firstname;
	}
	public void setFirstname(String firstname) {
		this.firstname = firstname;
	}
	public String getLastname() {
		return lastname;
	}
	public void setLastname(String lastname) {
		this.lastname = lastname;
	}
	public PhoneNumber getPhone() {
		return phone;
	}
	public void setPhone(PhoneNumber phone) {
		this.phone = phone;
	}
	public PhoneNumber getFax() {
		return fax;
	}
	public void setFax(PhoneNumber fax) {
		this.fax = fax;
	}
	 
	
}

public class PhoneNumber {
	private int code;
	private String number;
	
	public PhoneNumber(int code,String number){
		this.code = code;
		this.number = number;
	}
	
	public int getCode() {
		return code;
	}
	public void setCode(int code) {
		this.code = code;
	}
	public String getNumber() {
		return number;
	}
	public void setNumber(String number) {
		this.number = number;
	}
}
Copy after login

Tool class

import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.io.xml.StaxDriver;
import com.wind.study.entity.Person;
import com.wind.study.entity.PhoneNumber;

/**
 * 
* @author wind
* @date 2016年9月13日 下午4:49:32 
* @Description: bean/XML 互转
 */
public class BeanXMLConvertUtil {
	
	public static void main(String[] args) {
		XStream xstream = new XStream(new StaxDriver());
		
		//XStream的XML输出更简洁,可以为您的自定义类名创建别名XML元素名称。这是唯一类型的映射需要使用XStream甚至是可选的。
		xstream.alias("person", Person.class);
		xstream.alias("phonenumber", PhoneNumber.class);
		
		Person joe = new Person("Joe", "Walnes");
		joe.setPhone(new PhoneNumber(123, "1234-456"));
		joe.setFax(new PhoneNumber(123, "9999-999"));
		
		//bean to XML
		String xml = xstream.toXML(joe);
		//XML to bean
		Person newJoe = (Person)xstream.fromXML(xml);
		
		System.out.println(newJoe.getFirstname());
		
		System.out.println(xml);
	}
}
Copy after login

The above is the detailed content of Detailed introduction to JavaBean and XML mutual conversion tool class. 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!