Examples of XStream

Source: Internet
Author: User

Write a small example, easy to review later:

1 PackageCom.demo; 2 3Importjava.util.ArrayList; 4Importjava.util.List; 5 6Importorg.junit.Test; 7 8ImportCom.thoughtworks.xstream.XStream; 9 10 Public classXstreamdemo {11 PublicList<province>getprovince () {Province BJ =Newprovince ();Bj.setname ("Beijing")); List<city> Bjcitys =NewArraylist<city>(); City DCQ =NewCity ("Dongcheng District", "Dongchengqu"); City XCQ =NewCity ("Xicheng", "Xichengqu"); City BCQ =NewCity ("North District", "Beichengqu"); 18Bjcitys.add (DCQ);19Bjcitys.add (XCQ);20Bjcitys.add (BCQ);21stBj.setcitys (bjcitys);Province GD =Newprovince ();Bj.setname ("Guangdong"); List<city> Gdcitys =NewArraylist<city>(); City Gzs =NewCity ("Guangzhou", "Guangzhoushi"); City Szs =NewCity ("Shenzhen", "Shenzhenshi"); City CZs =NewCity ("Chaozhou", "Chaozhoushi"); 30Gdcitys.add (GZS);31Gdcitys.add (SZS);32Gdcitys.add (CZs);33Gd.setcitys (gdcitys);list<province> provinces =NewArraylist<province>(); 36Provinces.add (BJ);37Provinces.add (GD);38returnprovinces;39     } 40/**41 * Convert JavaBean to XML string using XStream*/43@Test44 Public voidfun1 () {XStream xs =NewXStream ();String s =Xs.toxml (Getprovince ());47System.out.println (s);48     } 49 50 51/**52 * Use XStream alias (String Name,class object); method change the name of the class*/54@Test55 Public voidfun2 () {XStream xs =NewXStream ();Xs.alias ("China", List.class); Xs.alias ("Province", province.class); Xs.alias ("City"), City.class); A String s =Xs.toxml (Getprovince ());61System.out.println (s);62     } 63 64/**65 * Handle element becomes element attribute*/67@Test68 Public voidFun3 () {XStream xs =NewXStream ();70//Change AliasesXs.alias ("China", List.class); Xs.alias ("Province", province.class); Xs.alias ("City"), City.class); 74//The handle element becomes an element attributeXs.useattributefor (province.class, "name"); The String s =Xs.toxml (Getprovince ());78System.out.println (s);79     } 80/**81 * Remove set attribute corresponding element*/83@Test84 Public voidFun4 () {XStream xs =NewXStream ();86//Change AliasesXs.alias ("China", List.class); Xs.alias ("Province", province.class); Xs.alias ("City"), City.class); 90//The handle element becomes an element attributeXs.useattributefor (province.class, "name"); 92//remove a collection property corresponding elementXs.addimplicitcollection (province.class, "Citys"); 94 String s =Xs.toxml (Getprovince ());95System.out.println (s);96     } 97/**98 * Remove set attribute corresponding element*/100@Test101 Public voidfun5 () {102 XStream xs =NewXStream ();103//Change Aliases104 Xs.alias ("China", List.class);Xs.alias ("Province", province.class);106 Xs.alias ("City"), City.class);107//The handle element becomes an element attribute108 Xs.useattributefor (province.class, "name");109//remove a collection property corresponding elementXs.addimplicitcollection (province.class, "Citys");111//to have a class member do not generate the corresponding XML elementXs.omitfield (city).class, "explain");113 String s =Xs.toxml (Getprovince ());114System.out.println (s);115     }116 117}

----Other classes involved-----

Provice.class

1Importjava.util.List;2 3 Public classProvince {4PrivateString name;5PrivateList<city>Citys;6 PublicProvince () {7Super(); 8//TODO auto-generated Constructor stub9     }10 PublicProvince (String name, list<city>citys) {11Super();12 This. Name =name;13 This. Citys =Citys;14     }15 PublicString GetName () {16returnname;17     }18 Public voidsetName (String name) {19 This. Name =name;20     }21st PublicList<city>Getcitys () {22returnCitys;23     }24 Public voidSetcitys (list<city>citys) {25 This. Citys =Citys;26     }27 28 29}

City.class

1 Public classCity {2PrivateString name;3PrivateString explain;4 PublicCity () {5Super(); 6//TODO auto-generated Constructor stub7     } 8 PublicCity (string name, String explain) {9Super();10 This. Name =name;11 This. explain =explain;12     }13 PublicString GetName () {14returnname;15     }16 Public voidsetName (String name) {17 This. Name =name;18     }19 PublicString Getexplain () {20returnexplain;21st     }22 Public voidSetexplain (String explain) {23 This. explain =explain;24     } 25 26}

Examples of XStream

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.