xstream对象xml互转

1、引入jar包

xpp3_min-1.1.4c.jar
xstream-1.4.8.jar

2、建立java bean

package com.jdw.bean;

import java.util.ArrayList;
import java.util.List;

public class Company {
	private List<Department> departments = new ArrayList<Department>();

	public List<Department> getDepartments() {
		return departments;
	}

	public void addDepartment(Department department) {
		this.departments.add(department);
	}

	@Override
	public String toString() {
		return "Company [departments=" + departments + "]";
	}

}
package com.jdw.bean;

import java.util.ArrayList;
import java.util.List;

public class Department {
	private String deptId;
	private String name;
	private List<Staff> staffs = new ArrayList<Staff>();

	public String getDeptId() {
		return deptId;
	}

	public void setDeptId(String deptId) {
		this.deptId = deptId;
	}

	public String getName() {
		return name;
	}

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

	public List<Staff> getStaffs() {
		return staffs;
	}

	public void addStaff(Staff staff) {
		this.staffs.add(staff);
	}

	@Override
	public String toString() {
		return "Department [deptId=" + deptId + ", name=" + name + ", staffs=" + staffs + "]";
	}
}

 

package com.jdw.bean;

public class Staff {
	private Integer id;
	private String name;
	private Integer age;
	private String address;
	private Double salary;

	@Override
	public String toString() {
		return "Staff [id=" + id + ", name=" + name + ", age=" + age + ", address=" + address + ", salary=" + salary
				+ "]";
	}

	public Staff() {
		super();
	}

	public Staff(Integer id, String name, Integer age, String address, Double salary) {
		super();
		this.id = id;
		this.name = name;
		this.age = age;
		this.address = address;
		this.salary = salary;
	}

	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 Integer getAge() {
		return age;
	}

	public void setAge(Integer age) {
		this.age = age;
	}

	public String getAddress() {
		return address;
	}

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

	public Double getSalary() {
		return salary;
	}

	public void setSalary(Double salary) {
		this.salary = salary;
	}

}

3、JunitTest

package com.jdw.test;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import com.jdw.bean.Company;
import com.jdw.bean.Department;
import com.jdw.bean.Staff;
import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.io.xml.DomDriver;

public class XmlTest {

	private XStream xstream;
	
	@Before
	public void setUp() throws Exception {
		xstream=new XStream(new DomDriver());
		xstream.alias("company", Company.class);
		xstream.alias("department", Department.class);
		xstream.alias("staff", Staff.class);
		
		xstream.aliasAttribute(Staff.class, "id","cc");
	}

	@After
	public void tearDown() throws Exception {
		
	}

	@Test
	public void test() {
		
		Company company=new Company();
		
		Department dept1=new Department();
		dept1.setDeptId("001");
		dept1.setName("技术部");
		dept1.addStaff(new Staff(1,"黄药师",40,"山东青岛",new Double(10000)));
		dept1.addStaff(new Staff(2,"郭靖",20,"山东济南",new Double(3000)));
		
		Department dept2=new Department();
		dept2.setDeptId("002");
		dept2.setName("销售部");
		dept2.addStaff(new Staff(1,"黄蓉",20,"山东青岛",new Double(5000)));
		dept2.addStaff(new Staff(2,"张三丰",30,"山东济南",new Double(6000)));
		
		company.addDepartment(dept1);
		company.addDepartment(dept2);
		
		String str="company.xml";
		
		try {
			PrintWriter pw=new PrintWriter(str,"UTF-8");
			
			xstream.toXML(company, pw);
			
		} catch (FileNotFoundException | UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}

	@Test
	public void testRead(){
		try {
			InputStream in=new FileInputStream(new File("company.xml"));
			Company company=(Company)xstream.fromXML(in);
			System.out.println(company);
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}
}

  

posted @ 2016-05-19 22:26  御灵手  阅读(325)  评论(0编辑  收藏  举报