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();
} }
}

  

xstream对象xml互转的更多相关文章

  1. JAVA bean与XML互转的利器---XStream

    最近在项目中遇到了JAVA bean 和XML互转的需求, 本来准备循规蹈矩使用dom4j忽然想起来之前曾接触过的XStream, 一番研究豁然开朗,利器啊利器, 下来就XStream的一些用法与大家 ...

  2. Xstream(对象和xml转换)

    package com.vcredit.framework.utils; import java.io.Writer; import org.apache.commons.lang3.StringUt ...

  3. XML和对象属性互转的基类

    本人做了一个XML和对象属性互转的基类,现在放上来有兴趣拿去看一下,用法很简单,声明一个BaseConversion的子类,该子类与你想转换的对象相对应,然后覆盖基类的两个虚方法,然后在里面写元素与对 ...

  4. XML解析---利用XStream解析xml数据及反构造Java对象

    XStream 是一个轻量级的.简单易用的开放源代码 Java库,用于将 Java 对象序列化为 XML 或者再转换回来.而且XStream还能将java对象转成其它格式,比如JSon. 需要用到的包 ...

  5. json、javaBean、xml互转的几种工具介绍

    json.javaBean.xml互转的几种工具介绍 转载至:http://blog.csdn.net/sdyy321/article/details/7024236 工作中经常要用到Json.Jav ...

  6. C#中另辟蹊径解决JSON / XML互转的问题

    C#中另辟蹊径解决JSON / XML互转的问题 最近在一个POC的项目中要用到JSON和XML的相互转换, 虽然我知道很多类库如JSON.NET具备这种功能, 但是我还是另辟蹊径的使用Spider ...

  7. JavaBean与xml互转的方法详解

    本文实例讲述了Jaxb2实现JavaBean与xml互转的方法.分享给大家供大家参考,具体如下: 一.简介 JAXB(Java Architecture for XML Binding) 是一个业界的 ...

  8. Springboot中使用Xstream进行XML与Bean 相互转换

    在现今的项目开发中,虽然数据的传输大部分都是用json格式来进行传输,但是xml毕竟也会有一些老的项目在进行使用,正常的老式方法是通过获取节点来进行一系列操作,个人感觉太过于复杂.繁琐.推荐一套简单的 ...

  9. Jaxb2实现JavaBean与xml互转的方法详解

    本文实例讲述了Jaxb2实现JavaBean与xml互转的方法.分享给大家供大家参考,具体如下: 一.简介 JAXB(Java Architecture for XML Binding) 是一个业界的 ...

随机推荐

  1. js戳和php戳时间换算

    问题:剩余多少时间,如果只用php来输出,却看不到动态效果.解决办法,利用获取的时间减去当前时间js 时间格式转换php时间商品距离秒杀时间的天数时分秒<input name="tes ...

  2. 用IO流拷贝歌曲

    package lianxi; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOEx ...

  3. ACM题目推荐(刘汝佳书上出现的一些题目)[非原创]

    原地址:http://blog.csdn.net/hncqp/article/details/1758337 推荐一些题目,希望对参与ICPC竞赛的同学有所帮助. POJ上一些题目在http://16 ...

  4. NOI十连测 第四测 T2

    思路:线段树套可持久化treap,可持久化treap我还是第一次听说.. 改题的时候没看数据范围..乱开数组T_T #include<algorithm> #include<cstd ...

  5. JAVA的类加载器,详细解释

    JVM规范定义了两种类型的类装载器:启动内装载器(bootstrap)和用户自定义装载器(user-defined class loader). 一. ClassLoader基本概念 1.ClassL ...

  6. Linux系统编程(12)——shell基础

    Shell的作用是解释执行用户的命令,用户输入一条命令,Shell就解释执行一条,这种方式称为交互式(Interactive),Shell还有一种执行命令的方式称为批处理(Batch),用户事先写一个 ...

  7. bzoj1633 [Usaco2007 Feb]The Cow Lexicon 牛的词典

    Description 没有几个人知道,奶牛有她们自己的字典,里面的有W (1 ≤ W ≤ 600)个词,每个词的长度不超过25,且由小写字母组成.她们在交流时,由于各种原因,用词总是不那么准确.比如 ...

  8. java使用poi创建excel文件

    import org.apache.poi.hssf.usermodel.HSSFCell;import org.apache.poi.hssf.usermodel.HSSFRow;import or ...

  9. 剑指offer-面试题15.链表中倒数第k个结点

    题目:输入一个链表,输出该链表的倒数第K个结点.为了符合大多数人的习惯,本题 从1开始计数,即链表的尾结点是倒数第1个节点.例如有一个链表有6个节点,从 头节点开始他们的值依次是1,2,3,4,5,6 ...

  10. HDU5441 Travel (离线操作+并查集)

    Travel Time Limit: 1500/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)Total Su ...