jaxb的序列化和反序列化
@XmlRootElement(name="company")
public class Company {
// @XmlElement(name="cname")
private String cname;
@XmlElement(name="employee")
private List<Employee> employees;
public Company(String cname,List<Employee> employees){
this.cname=cname;
this.employees=employees;
}
public Company(){}
@XmlTransient
public List<Employee> getEmployees() {
return this.employees;
}
public void setEmployees(List<Employee> employees) {
this.employees = employees;
}
// public void addEmployee(Employee employee){
// if(employees==null){
// this.employees=new ArrayList<Employee>();
// }
// this.employees.add(employee);
// }
public String getCname() {
return this.cname;
}
public void setCname(String cname) {
this.cname = cname;
}
}
@XmlType
public class Employee {
@XmlElement(name="name")
private String name;
@XmlElement(name="id")
private String id;
public Employee(String name,String id){
this.id=id;
this.name=name;
}
public Employee(){}
@XmlTransient
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@XmlTransient
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
}
测试类:
public class Test {
public static void main(String[] args){
Test.test01();
}
public static void test01() {
long l=System.currentTimeMillis();
try {
JAXBContext ctx = JAXBContext.newInstance(Company.class);
Marshaller marshaller = ctx.createMarshaller();
List<Employee> list=new ArrayList<Employee>();
list.add(new Employee("1","1e"));
list.add(new Employee("2", "2e"));
Company c = new Company("cc",list);
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
// marshaller.marshal( c,new FileOutputStream("src/"+l+".xml"));
StringWriter sw = new StringWriter();
marshaller.marshal(c,sw);
System.out.println(sw.toString());
test02(sw.toString());
// Unmarshaller unmarshaller = ctx.createUnmarshaller();
// File file = new File("src/"+l+".xml");
// Company cc = (Company)unmarshaller.unmarshal(file);
// // System.out.println(cc.getCname());
// System.out.println(cc.getEmployees().get(0).getName());
// System.out.println(file.exists());
} catch (JAXBException e) {
e.printStackTrace();
}
}
public static void test02(String xml) {
try {
// String xml = "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?><company><employee><name>1</name><id>1e</id></employee><employee><name>2</name><id>2e</id></employee><cname>cc</cname></company>";
JAXBContext ctx = JAXBContext.newInstance(Company.class);
Unmarshaller um = ctx.createUnmarshaller();
Company stu = (Company)um.unmarshal(new StringReader(xml));
System.out.println(stu.getEmployees().get(0).getName() +","+stu.getCname());
} catch (JAXBException e) {
e.printStackTrace();
}
}
public static void test03() {
try{
JAXBContext context = JAXBContext.newInstance(Company.class);
Unmarshaller unmarshaller = context.createUnmarshaller();
File file = new File("src/test.xml");
Company c = (Company)unmarshaller.unmarshal(file);
System.out.println(c.getCname());
System.out.println(c.getEmployees().get(0).getName());
}catch (Exception e){
}
// System.out.println(people.age);
}
}