Android Base--xml serialization (XmlSerializer)

Source: Internet
Author: User

1. JavaBean of the Encapsulation node

 Public classCity {PrivateString name; PrivateString temp; PrivateString PM25;  PublicString GetName () {returnname; }     Public voidsetName (String name) { This. Name =name; }     PublicString gettemp () {returntemp; }     Public voidsettemp (String temp) { This. temp =temp; }     PublicString getPm25 () {returnPM25; }     Public voidsetPm25 (String pm25) { This. PM25 =PM25; } @Override PublicString toString () {return"City [name=" + name + ", temp=" + temp + ", pm25=" + PM25 + "]"; }     PublicCity (string name, String temp, string pm25) {Super();  This. Name =name;  This. temp =temp;  This. PM25 =PM25; }     PublicCity () {Super(); }    }

2. Generate XML Mainactivity.java, where Createxmlfile is a click event bound on a button control

 PackageCom.itheima.xmlserializer;ImportJava.io.File;ImportJava.io.FileOutputStream;ImportJava.io.OutputStream;Importjava.util.ArrayList;Importjava.util.List;ImportOrg.xmlpull.v1.XmlSerializer;Importandroid.app.Activity;ImportAndroid.os.Bundle;Importandroid.os.Environment;Importandroid.util.Xml;ImportAndroid.view.View;Importcom.itheima.xmlserializer.domain.City; Public classMainactivityextendsActivity {List<City>CityList; @Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate);        Setcontentview (R.layout.activity_main); CityList=NewArraylist<city>(); City City=NULL;  for(inti = 0; I < 5; i++) { City=NewCity ("Cities" + I, + i + "", + + i + ""));        Citylist.add (city); }    }     Public voidCreatexmlfile (View v) {//determine if the current SD card is available        if(Environment.getexternalstoragestate (). Equals (environment.media_mounted)) {//specify where to store the XML fileFile File =NewFile (Environment.getexternalstoragedirectory (),"Info.xml"); //get the XML serializerXmlSerializer xs =Xml.newserializer (); OutputStream OS=NULL; Try {                //outputstream: Specify the save path for the file//Encoding: Specifies the encoding of the generated XML fileOS =Newfileoutputstream (file); Xs.setoutput (OS,"UTF-8"); //To start generating files//set the first line of the XML file <?xml encoding =? standalone=?>Xs.startdocument ("UTF-8",true); //generate root node start tagXs.starttag (NULL, "Weather");  for(city city:citylist) {Xs.starttag (NULL, "City"); Xs.starttag (NULL, "name"); //Generating text nodesXs.text (City.getname ()); Xs.endtag (NULL, "name"); Xs.starttag (NULL, "temp"); //Generating text nodesXs.text (City.gettemp ()); Xs.endtag (NULL, "temp"); Xs.starttag (NULL, "PM25"); //Generating text nodesXs.text (CITY.GETPM25 ()); Xs.endtag (NULL, "PM25"); Xs.endtag (NULL, "City"); }                //Generate root node end tagXs.endtag (NULL, "Weather");            Xs.enddocument (); } Catch(Exception e) {Throw NewRuntimeException (e); }        }    }}

3. The resulting XML file is as follows, because there is no line break and all readability is poor

<?XML version= ' 1.0 ' encoding= ' UTF-8 ' standalone= ' yes '?><Weather>< City><name>City 0</name><Temp>20</Temp><PM25>100</PM25></ City>< City><name>City 1</name><Temp>21st</Temp><PM25>101</PM25></ City>< City><name>City 2</name><Temp>22</Temp><PM25>102</PM25></ City>< City><name>City 3</name><Temp>23</Temp><PM25>103</PM25></ City>< City><name>City 4</name><Temp>24</Temp><PM25>104</PM25></ City></Weather>

Android Base--xml serialization (XmlSerializer)

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.