Android Build and Pull parsing xml

Source: Internet
Author: User

One, single object generation XML

Generate the following XML, how to generate it?

<?xml version= ' 1.0 ' encoding= ' UTF-8 ' standalone= ' yes '?><account>  <id>1</id>  < Password>123456</password>  <name> Legendary beauty </name>  <createdate>2015-02-02 11:50:42 </createDate></account>

First define an account class with the attributes ID, name, password, createdate.

public class Account {private String ID;    private String password;    private String name;    Private String CreateDate;    Public account () {super ();        The public account (string ID, string password, string name, String createdate) {super ();        This.id = ID;        This.password = password;        THIS.name = name;    This.createdate = CreateDate;    } public String GetId () {return id;    } public void SetId (String id) {this.id = ID;    } public String GetPassword () {return password;    } public void SetPassword (String password) {this.password = password;    } public String GetName () {return name;    } public void SetName (String name) {this.name = name;    } public String Getcreatedate () {return createdate;    } public void Setcreatedate (String createdate) {this.createdate = CreateDate; } @Override Public String toString () {return ' account [id= ' + ID+ ", password=" + password + ", name=" + name + ", createdate=" + createdate + "]\n\n"; }}

This class is defined so that you can use XmlSerializer to write xml data . Write a method to save the generated XML in the Xmlparser_account.xml file.

/** * Single Object generation XML * @param account */private static void Xmlfilecreator (account account) {file Newxmlfile = new File (Environ Ment.getexternalstoragedirectory () + "/xmlparser_account.xml"); try {if (!newxmlfile.exists ()) Newxmlfile.createnewfile ();} catch (IOException e) {log.e ("IOException", "Exception in CreateNewFile () method"); FileOutputStream Fileos = null;try {Fileos = new FileOutputStream (newxmlfile);} catch (FileNotFoundException e) {Log.e ("F Ilenotfoundexception "," Can ' t create FileOutputStream ");} XmlSerializer for writing Xml data XmlSerializer serializer = Xml.newserializer (); try {//XmlSerializer with UTF-8 Encode Serializer.setoutput (Fileos, "UTF-8"); Serializer.startdocument (null, boolean.valueof (true)); Serializer.setfeature ("Http://xmlpull.org/v1/doc/features.html#indent-output", true); Serializer.starttag (null, " Account ");//Xml-tree, starting with Starttag, Endtag end Serializer.starttag (NULL," id "); Serializer.text (Account.getid ()); Serializer.endtag (NULL, "id"); Serializer.starttag (null, "password"); serializer.tExt (Account.getpassword ()); Serializer.endtag (null, "password"); Serializer.starttag (null, "name"); Serializer.text (Account.getname ()); Serializer.endtag (null, "name"), Serializer.starttag (null, "CreateDate"); Serializer.text ( Account.getcreatedate ()); Serializer.endtag (null, "CreateDate"); Serializer.endtag (null, "account"); Serializer.enddocument ();//write XML data to Fileoutputstreamserializer.flush ();//close Fileos, Release resource Fileos.close ();} catch (Exception e) {log.e ("Exception", "error occurred while creating XML file");}}

Generate account object, single object generates XML

SimpleDateFormat SDF = new SimpleDateFormat ("Yyyy-mm-dd hh:mm:ss"); Account Account = new Account ("1", "123456", "Legendary Beauty", Sdf.format (New Date ())); Xmlfilecreator (account);

To view saved files

Second, parse a single object composed of XML as a single object

Put the generated xmlparser_account.xml file under Res/xml/and parse the XML into the account object. Here, Xmlresourceparser,xmlresourceparser inherits the Xmlpullparse class.

/** * Parse XML and XML groups consisting of a single object * @return */private list<account> getlistdata () {list<account> accountlist = new Array List<account> (); Xmlresourceparser XRP = Getresources (). GETXML (R.xml.xmlparser_account); try {//until the end of the document account account = Null;while ( Xrp.geteventtype ()! = xmlresourceparser.end_document) {String tagName = Xrp.getname (); if (xrp.geteventtype () = = Xmlresourceparser.start_document) {}//If a start tag is encountered if (xrp.geteventtype () = = Xmlresourceparser.start_tag) {Log.i ("", TagName), if (Tagname.equals ("account")) {account = new account (),} else if (account! = null) {if (Tagname.equals ("id")) {St Ring id = xrp.nexttext ();//Get property value Account.setid (ID) by property name;} else if (tagname.equals ("password")) {String password = xrp.nexttext ();//Property index to get property value Account.setpassword (password);} else if (tagname.equals ("name")) {String name = Xrp.nexttext (); Account.setname (name);} else if (Tagname.equals (" CreateDate ")) {String createdate = Xrp.nexttext (); account.setcreatedate (CreateDate);}}} if (Xrp.geteventtype () = = Xmlresourceparser.end_tag) {if (Tagname.equals ("account") && account!=null) {Accountlist.add (account ); account = null;}} Xrp.next ();//Get Parse Next event}} catch (Xmlpullparserexception e) {e.printstacktrace ();} catch (IOException e) { E.printstacktrace ();} return accountlist;}

Direct Print results See

LOG.I ("", Getlistdata (). toString ());

Log as follows

Third, an XML group consisting of a single object

Like this

<?xml version= ' 1.0 ' encoding= ' UTF-8 ' standalone= ' yes '?><accounts>  <account>    <id>2 </id>    <password>123456</password>    <name> legends </name>    <createDate> 2015-02-02 02:54:41</createdate>  </account>  <account>    <id>3</id>    <password>567890</password>    <name> Beauty </name>    <createdate>2015-02-02 02:54:41</createdate>  </account></accounts>

Generating a single object group consists of an XML group that is almost the same as a single object XML, written as a method to save the generated XML in the Xmlparser_accounts.xml file.

/** * Generate an XML array of individual objects * * @param data */private static void Xmlfilecreator (list<account> data) {File newxmlfile = new File (Environment.getexternalstoragedirectory () + "/xmlparser_accounts.xml"), try {if (!newxmlfile.exists ()) Newxmlfile.createnewfile ();} catch (IOException e) {log.e ("IOException", "Exception in CreateNewFile () method"); FileOutputStream Fileos = null;try {Fileos = new FileOutputStream (newxmlfile);} catch (FileNotFoundException e) {Log.e ("F Ilenotfoundexception "," Can ' t create FileOutputStream ");} XmlSerializer serializer = Xml.newserializer (); try {serializer.setoutput (Fileos, "UTF-8"); Serializer.startdocument ( NULL, boolean.valueof (TRUE)); Serializer.setfeature ("Http://xmlpull.org/v1/doc/features.html#indent-output", True ); Serializer.starttag (null, "accounts"); for (account Account:data) {Serializer.starttag (null, "account"); Serializer.starttag (NULL, "id"); Serializer.text (Account.getid ()); Serializer.endtag (NULL, "id"); Serializer.starttag (NULL, "password"); Serializer.texT (Account.getpassword ()); Serializer.endtag (null, "password"); Serializer.starttag (null, "name"); Serializer.text ( Account.getname ()); Serializer.endtag (null, "name"), Serializer.starttag (null, "CreateDate"); Serializer.text ( Account.getcreatedate ()); Serializer.endtag (null, "CreateDate"); Serializer.endtag (null, "account"); Serializer.endtag (NULL, "accounts"); Serializer.enddocument (); Serializer.flush (); Fileos.close ();} catch (Exception e) {log.e ("Exception", "error occurred while creating XML file");}}

Simple to generate with a few lines of code

Account Account1 = new Account ("2", "123456", "Legend", Sdf.format (New Date ())); Account Account2 = new Account ("3", "567890", "Beauty", Sdf.format (New Date ())); list<account> accountlist = new arraylist<account> (); Accountlist.add (account1); Accountlist.add ( ACCOUNT2); Xmlfilecreator (accountlist);

The resulting files are as follows

Iv. parsing an XML group consisting of a single object

As with two, the XML that parses a single object is a single object , see two

V. Generating an XML group consisting of a single object with attribute

Similar to the following, the account also contains a attribute value how to generate, in fact, it is very simple, in three, a single object composed of an XML group to modify a little on the basis of

<?xml version= ' 1.0 ' encoding= ' UTF-8 ' standalone= ' yes '?><accounts>  <account id= ' 2 ' >    < password>123456</password>    <name> Legends </name>    <createdate>2015-02-02 04:50:45 </createDate>  </account>  <account id= "3" >    <password>567890</password>    <name> Beauty </name>    <createdate>2015-02-02 04:50:45</createdate>  </account ></accounts>

Modify place for

for (account Account:data) {Serializer.starttag (null, "account"), Serializer.attribute (NULL, "id", Account.getid ()); /serializer.starttag (NULL, "id");//serializer.text (Account.getid ());//serializer.endtag (NULL, "id"); Serializer.starttag (NULL, "password"); Serializer.text (Account.getpassword ()); Serializer.endtag (null, "password") ; Serializer.starttag (NULL, "name"); Serializer.text (Account.getname ()); Serializer.endtag (null, "name"); Serializer.starttag (NULL, "CreateDate"); Serializer.text (Account.getcreatedate ()); Serializer.endtag (null, " CreateDate "); Serializer.endtag (null," account ");}

VI. parsing an XML group consisting of a single object with attribute

Parse the same, with four, parse a single object composed of an XML group , modify the ID part of the resolution can be

If a start tag is encountered if (xrp.geteventtype () = = Xmlresourceparser.start_tag) {log.i ("", TagName), if (Tagname.equals ("account")) {account = new account (); String id = xrp.getattributevalue (NULL, "id"); Account.setid (ID);} else if (account! = null) {if (Tagname.equals ("id")) {//string id = xrp.nexttext ();//account.setid (ID);} else if (tagName. Equals ("password")) {String password = xrp.nexttext (); Account.setpassword (password);} else if (Tagname.equals ("name") {String name = Xrp.nexttext (); Account.setname (name);} else if (tagname.equals ("CreateDate")) {String createdate = Xrp.nexttext (); account.setcreatedate (CreateDate);}}}

This article original link: http://www.cnblogs.com/liqw/p/4267461.html

: http://download.csdn.net/detail/lqw770737185/8417583

Android Build and Pull parsing xml

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.