使用xstream實現對JavaBean與xml字串的互轉

iteye_20824發表於2012-05-11

近來專案中需要實現javabean與xml檔案的互轉,一個同事發現了xstream不錯。簡潔易用,不錯的開源元件,在這裡推薦一下。並附上使用示例與jar包作為筆記。有需要的同學可以學學。

 

1.Person javabean類

 

public class Person {
	private String firstname;
	private String lastname;
	private PhoneNumber phone;
	private PhoneNumber fax;

	public Person() {
		super();
	}

	public Person(String firstname, String lastname) {
		super();
		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;
	}

	@Override
	public String toString() {
		return "Person [firstname=" + firstname + ", lastname=" + lastname
				+ ", phone=" + phone + ", fax=" + fax + "]";
	}

}

 


2.PhoneNumber javabean類

public class PhoneNumber {
	private int code;
	private String number;

	public PhoneNumber() {
		super();
	}

	public PhoneNumber(int code, String number) {
		super();
		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;
	}

	@Override
	public String toString() {
		return "PhoneNumber [ code =" + code + ", number=" + number + "]";
	}


3.轉換測試類

import org.junit.Test;
import com.thoughtworks.xstream.XStream;

public class ConvertTest {

	/**
	 * 將Person轉換成xml字串
	 */
	@Test
	public void personToXML() {
		XStream xstream = new 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"));

		String xml = xstream.toXML(joe);
		System.out.println(xml);
	}
        //輸出結果
	/*
	 * <person>
  <firstname>Joe</firstname>
  <lastname>Walnes</lastname>
  <phone>
    <code>123</code>
    <number>1234-456</number>
  </phone>
  <fax>
    <code>123</code>
    <number>9999-999</number>
  </fax>
</person>
	 */

	/**
	 * 將xml字串轉換成Person
	 */
	@Test
	public void xmlToPerson() {
		String site = "CAN";
		XStream xstream = new XStream();
		xstream.alias("person", Person.class);
		xstream.alias("phonenumber", PhoneNumber.class);

		String xml = "<person><firstname>Joe</firstname><lastname>Walnes</lastname><phone><code>123</code><number>1234-456</number></phone><fax><code>123</code><number>9999-999</number></fax></person>";
		xstream.omitField(Person.class, site);
		Person newJoe = (Person) xstream.fromXML(xml);
		System.out.println(newJoe.toString());
	}
        //輸出結果
	//Person [firstname=Joe, lastname=Walnes, phone=PhoneNumber [ code = 123, number=1234-456],
        // fax=PhoneNumber [ code =123, number=9999-999]]

}
 
官網:http://xstream.codehaus.org/tutorial.html


參考網址:http://www.cnblogs.com/hoojo/archive/2011/04/22/2025197.html



 

相關文章