---> 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