Java Bean to XML

Source: Internet
Author: User

Province class

Package Com.apptest.xstream.test;import Java.util.arraylist;import Java.util.list;public class Province {private String name;private list<city> List = new arraylist<city> ();p ublic String getName () {return name;} public void SetName (String name) {this.name = name;} Public list<city> getList () {return List;} public void setlist (list<city> list) {this.list = list,} public void Addcity (city) {this.list.add (city);} @Overridepublic String toString () {return "province [name=" + name + "]";}}

City class

Package Com.apptest.xstream.test;public class City {private string Name;private string Description;public City (string Name, String description) {super (); this.name = Name;this.description = description;} Public String GetName () {return name;} public void SetName (String name) {this.name = name;} Public String GetDescription () {return description;} public void SetDescription (String description) {this.description = description;} @Overridepublic String toString () {return "City [name=" + name + ", description=" + description + "]";}}

Test class

Package Com.apptest.xstream.test;import Java.util.arraylist;import Java.util.list;import org.junit.Test;import Com.thoughtworks.xstream.xstream;public class Xstreamdemo {/** * get province list * @return arraylist<province> list */ Public list<province> Getprovincelist () {Province P1 = new province ();p 1.setName ("Jiangxi");p 1.addCity ("Pingxiang" , "Pingxiang"));p 1.addCity (New City ("Shangrao", "Shangrao")); Province P2 = new province ();p 2.setName ("Shanghai");p 2.addCity (New City ("Zhabei", "Zhabeiqu");p 2.addCity ("Xuhui", " Xuhuiqu ")); list<province> list = new arraylist<province> (); List.add (p1); List.add (p2); return list;} @Testpublic void Func1 () {list<province> List = Getprovincelist (); XStream XStream = new XStream () Xstream.alias ("China", List.class); Xstream.alias ("Province", Province.class); Xstream.alias ("City", City.class); Xstream.useattributefor (Province.class, "name"); Xstream.addimplicitcollection ( Province.class, "list"); Xstream.omitfield (City.class, "description"); String s = xstreAm.toxml (list); System.out.println (s);}}

Result

<china>  <province name= "Jiangxi" >    <city>      <name> Pingxiang </name>    </city >    <city>      <name> Shangrao </name>    </city>  </province>  < Province name= "Shanghai" >    <city>      <name> Zhabei </name>    </city>    <city>      <name> Xuhui </name>    </city>  </province></china>

  

Java Bean to XML

Related Article

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.