Package com. nexus. XStreamTest; import java. io. writer; import java. util. arrayList; import java. util. list; import com. nexus. XStream. address; import com. nexus. XStream. person; import com. nexus. XStream. profile; import com. thoughtworks. xstream. XStream; import com. thoughtworks. xstream. core. util. quickWriter; import com. thoughtworks. xstream. io. hierarchicalStreamWriter; import com. thoughtworks. xstream. io. xml. prettyPrintWriter; import com. thoughtworks. xstream. io. xml. xppDriver; public class Test {/*** @ param args */public static void main (String [] args) {/* User user = new User (); user. setId ("id"); user. setName ("name"); user. setUrl ("url"); xstream. alias ("xml", user. getClass (); // add xstream to the method for processing objects. autodetectAnnotations (true); this will solve this problem // xstream. autodetectAnnotations (true); System. out. println (xstream. toXML (user); */Address address1 = new Address ("Zhengzhou Jingsan Road", "450001"); Address address2 = new Address ("Yanta Road, Xi'an ", "710002"); List <Address> addList = new ArrayList <Address> (); addList. add (address1); addList. add (address2); Profile profile = new Profile ("software engineer", "13512129933", "remarks"); Person person = new Person ("Lava", "27 ", profile, addList); xstream. alias ("PERSON", Person. class); xstream. alias ("PROFILE", Profile. class); xstream. alias ("ADDRESS", Address. class); System. out. println (xstream. toXML (person);}/*** initialization, encapsulated application */private static XStream xstream = new XStream (new XppDriver () {public HierarchicalStreamWriter createWriter (Writer out) {return new PrettyPrintWriter (out) {public void startNode (String name, Class clazz) {super. startNode (name);} protected void writeText (QuickWriter writer, String text) {/** custom formatting **/writer. write (text );}};}});}
Package com. nexus. XStreamTest; import com. thoughtworks. xstream. annotations. XStreamOmitField; public class User {// add @ XStreamOmitField annotation @ XStreamOmitField private String id; private String name; private String url; public String getId () to the ignored field attribute or method () {return id;} public void setId (String id) {this. id = id;} public String getName () {return name;} public void setName (String name) {this. name = name;} public String getUrl () {return url;} public void setUrl (String url) {this. url = url ;}}
Package com. nexus. 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 com. nexus. 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 com. nexus. XStream; public class Address {private String add; private String zipcode; public Address (String add, String zipcode) {this. add = add; this.zip code = zipcode;} public String toString () {return "Address {" + "add = '" + add +' \ ''+ ", zipcode = '"+ zipcode +' \'' + '}';}}
Xstream serialized object is XML