对XStream的实例应用1

package com.mashen.servlet;
/**
 * 员工对象
 * @author Administrator
 *
 */

public class Employee {
private int id;
private String name;
private int age;
private String sex;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}


}


package com.mashen.servlet;


import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;
import com.thoughtworks.xstream.XStream;
/**
 *部门 对象
 * @author Administrator
 *
 */

public class Department {
private int code;
private String name;
private  Set<Employee> Employee ;
public int getCode() {
return code;
}
public void setCode(int code) {
this.code = code;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}

public Set<Employee> getEmployee() {
return Employee;
}
public void setEmployee(Set<Employee> employee) {
Employee = employee;
}
/**
* 对XStream的实例应用
* 将对象转换成XML文件格式
* @param args
*/

public static void main(String[] args) {
Employee ely1 = new Employee();
ely1.setId(1);
ely1.setName("小明");
ely1.setAge(20);
ely1.setSex("男");

Department depm = new Department();
depm.setCode(1);
depm.setName("技术部");

Set<Employee> emplySet = new HashSet<Employee>();
emplySet.add(ely1);

depm.setEmployee(emplySet);

XStream xst = new XStream();
xst.alias("employee", Employee.class);//起别名映射
xst.alias("department", Department.class);
String xml = xst.toXML(depm);
Department dept = (Department) xst.fromXML(xml);
System.out.println(dept);
Set<Employee> s = depm.getEmployee();
for (Iterator<Employee> iterator = s.iterator(); iterator.hasNext();) {
Employee obj = (Employee) iterator.next();

System.out.println(obj.getId()+'\n'+obj.getName()+'\n'+obj.getAge()+'\n'+obj.getSex());
}
}


}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值