开源项目Xstream简介3

XStream.NO_REFERENCES
    这种情况将失去对图形对象的支持,仅把对象看作为树型结构。重复的引用被视作两个不同的对象,循环引用会导致异常产生。相对于上面两种模式,这种模式速度 会更快,占用内存会更
  
    Facade(统一入口)
    主要类XStream用作所有项目的入口点。它将上面 所提及的重要组件集成在一起,提供更简单易用的API操作。

实例讲 解
package xstream;

public class Address {
private String add;
private String zipcode;
public Address(String add, String zipcode) {
this.add = add;
this.zipcode = zipcode;
}

public String toString() {
return "Address{" +
"add='" + add + '\'' +
", zipcode='" + zipcode + '\'' +
'}';
}
}
package xstream;


public class Profile {
private String job;
private String tel;
private String remark;
public Profile(String job, String tel, String remark) {
this.job = job;
this.tel = tel;
this.remark = remark;
}
public String toString() {
return "Profile{" +
"job='" + job + '\'' +
", tel='" + tel + '\'' +
", remark='" + remark + '\'' +
'}';
}
}
package xstream;

import java.util.List;

public class Person {
private String name;
private String age;
private Profile profile;
private List<Address> addlist;
public Person(String name, String age, Profile profile, List<Address> addlist) {
this.name = name;
this.age = age;
this.profile = profile;
this.addlist = addlist;
}
public String toString() {
return "Person{" +
"name='" + name + '\'' +
", age='" + age + '\'' +
", profile=" + profile +
", addlist=" + addlist +
'}';
}
}
package xstream;

import com.thoughtworks.xstream.XStream;
import java.util.List;
import java.util.ArrayList;

public class TestXStream {

public static void main(String[] args) {
test();
}
public static void test() {

//目标对象
Address address1 = new Address("郑州市经三路", "450001");
Address address2 = new Address("西安市雁塔路", "710002");
List<Address> addList = new ArrayList<Address>();
addList.add(address1);
addList.add(address2);
Profile profile = new Profile("软件工程师", "13512129933", "备注说明");
Person person = new Person("熔岩", "27", profile, addList);
//转换装配
XStream xStream = new XStream();
/************** 设置类别名 ****************/
xStream.alias("PERSON", Person.class);
xStream.alias("PROFILE", Profile.class);
xStream.alias("ADDRESS", Address.class);
output(1, xStream, person);

你可能感兴趣的:(xstream)