Xstream serialized object is XML

Source: Internet
Author: User
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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.