Xstream--> aliases; element to attribute; Remove set attribute (peel); ignore element not required

Source: Internet
Author: User

---> Checklist:

City.java

Province.java

Testxstream.java

4 Methods of XStream

Javabean-->city.java

1  PackageXStream;2 3  Public classCity {4     PrivateString name;5     PrivateString description;6     7      PublicString GetName () {8         returnname;9     }Ten      Public voidsetName (String name) { One          This. Name =name; A     } -      PublicString getdescription () { -         returndescription; the     } -      Public voidsetdescription (String description) { -          This. Description =description; -     } +      PublicCity (string name, string description) { -          This. Name =name; +          This. Description =description; A     } at}

Javabean-->province.java

1  PackageXStream;2 3 Importjava.util.ArrayList;4 Importjava.util.List;5 6  Public classProvince {7     PrivateString name;8     PrivateList<city> cities =NewArraylist<city>();9     Ten      Public voidaddcity (city) { One Cities.add (city); A     } -      -      PublicString GetName () { the         returnname; -     } -      Public voidsetName (String name) { -          This. Name =name; +     } -      PublicList<city>getcities () { +         returncities; A     } at      Public voidSetcities (list<city>cities) { -          This. Cities =cities; -     } -      PublicProvince (String name, list<city>cities) { -         Super(); -          This. Name =name; in          This. Cities =cities; -     } to  +      PublicProvince () {} -  the      PublicProvince (String name) { *          This. Name =name; $     }Panax Notoginseng}

Test class-->testxstream.java

1  PackageXStream;2 3 Importjava.util.ArrayList;4 Importjava.util.List;5 6 Importorg.junit.Test;7 8 ImportCom.thoughtworks.xstream.XStream;9 Ten  Public classTestxstream { One      PublicList<province>getprovincelist () { AList<province> provincelist =NewArraylist<province>(); -          -Province P1 =NewProvince ("Beijing")); theP1.addcity (NewCity ("Dongcheng District", "Dongcheng District")); -P1.addcity (NewCity ("Xicheng", "Xicheng")); -          -Province P2 =NewProvince ("Shanghai"); +P2.addcity (NewCity ("Xuhui", "Xuhui")); -P2.addcity (NewCity ("Pudong New Area", "Pudong New Area"))); +          A Provincelist.add (p1); at Provincelist.add (p2); -          -         returnprovincelist; -     } -     //---> Native edition - @Test in      Public voidFun () { -List<province> prolist =getprovincelist (); toXStream XStream =NewXStream (); +String XML =Xstream.toxml (prolist); - System.out.println (XML); the     } *  $     //---> ModificationPanax Notoginseng @Test -      Public voidfun1 () { theList<province> prolist =getprovincelist (); +XStream XStream =NewXStream (); A          the         //aliases +Xstream.alias ("China", java.util.List.class); -Xstream.alias ("Province", province.class); $Xstream.alias ("City"), City.class); $          -         //the handle element becomes the property of the parent element -Xstream.useattributefor (province.class, "name"); the          -         //Remove the container, "Peel"WuyiXstream.addimplicitcollection (province.class, "Cities"); the          -         //Ignore unwanted attribute-xml elements WuXstream.omitfield (city.class, "description"); -          AboutString XML =Xstream.toxml (prolist); $ System.out.println (XML); -     } -      -}

-->4 a XStream method.

---> Aliases

Convert---> child elements into attributes

---> Remove collection elements ("peel")

---> Ignore unwanted elements

Xstream--> aliases; element to attribute; Remove set attribute (peel); ignore element not required

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.