Android XML parsing

Source: Internet
Author: User

Android XML parsing

In Android development, often used to parse XML files, the common way to parse XML three kinds: SAX, pull, Dom parsing method.  sax Features: Parsing is an event-driven, consumption of memory less   pull features: parsing is an event-driven, consumption of memory, XML parsing inside the Android system is in this way   DOM features: DOM is to read all the files into memory, this way is very memory-intensive, for memory resources are very few Android phones, this method is not very recommended to use.   Personal advice:  sax and pull are more suitable for Mobile,dom server resolution, and as mobile phone memory increases, the DOM may be available.  sax Parse Example: Xml:[html] <?xml version= "1.0" encoding= "Utf-8"?>  <users>  <user name= "Admin0" age= ">root0</user>  <user name=" Admin1 "age=" >root1</user>  <user Name= "admin2" age= ">root2</user>  <user name=" admin3 "age=" >root3</user>  < /users>   in src/directory   saxhandler:[java]  package com.hualu.androidxml;    import java.util.ArrayList;  import java.util.List;    import org.xml.sax.Attributes;  import org.xml.sax.SAXException;  import Org.xml.sax.helpers.DefaultHandler;    public class Saxhandler EXtends DefaultHandler {       private list<user> users;      user user; & nbsp;    int i = 1;      @Override      public void startdocument () throws saxexception {     &N Bsp   users = new arraylist<user> ();                 @Override      public void enddocument ( ) throws Saxexception {         super.enddocument ();     }      & nbsp;     @Override      public void startelement (string uri, String localname, String qName ,              Attributes Attributes) throws Saxexception {                   if ("User". Equals (LocalName)) {           &NB Sp i = 2;              user = New User ();              User.setname (Attributes.getvalue ("name"));              User.setage (Attributes.getvalue ("Age"));         }               }      &nbs p;     @Override      public void endElement (string uri, String localname, string qName) &nb sp;            throws saxexception {         if ("User". Equals (Loca LName) {             users.add (user),         }  &nbsp ;  }            @Override      public void characters (char[] CH, int. Start, int length)              throws saxexception {       &NBSP ; if (i = = 2)              USER.SETPAssword (string.valueof (ch,start,length));                    i = 1;             public list<user> getusers () {       &nbs P return users;               }        }  saxclient:[java] &n Bsp;package Com.hualu.androidxml;    import Java.io.InputStream;  import java.util.List;    import Javax.xml.parsers.SAXParser;  import javax.xml.parsers.SAXParserFactory;    import Android.content.Context;    public class Saxclient {       public void Parserxml (context context) {&NBSP ;        try {             SAXParserFactory factory = Saxparserfac Tory.newinstance ();              SAXParser parser = Factory.newsaxparser ();              Saxhandler handler = new Saxhandler ();              InputStream file = SAXClient.class.getClassLoader (). getResourceAsStream ("User.xml");              Parser.parse (file, handler);              list<user> users = handler.getusers ();              for (User user:users) {             &NB Sp   System.out.println (User.getname () + ";" + User.getpassword () + " ;   "+ user.getage ());             }         } catch (Exception e) {   & nbsp    }         }   steps: 1, instantiate a factory saxparserfactory 2, instantiate Saxpraser object, Create XmlReader parser  3, instantiate handler, processor  4, register parser  4, read file stream  5, parse file   pull Parse example: xml:[html]  <?xml VersiOn= "1.0" encoding= "Utf-8"?>  <users>  <user name= "Admin0" age= "All" >root0</user>  <user name= "Admin1" age= ">root1</user>  <user name=" admin2 "age=" >root2</user " >  <user name= "admin3" age= "+" >root3</user>  </users>   placed in Res/xml directory.  pullxmlparser:[java]  package com.hualu.androidxml;    import java.util.ArrayList;  import java.util.List;    import android.app.Activity;  import Android.content.res.XmlResourceParser;    public class Pullxmlparser {           private activity activity; & nbsp;          public pullxmlparser (activity activity) {         T His.activity  = activity;                 public list<user> parser () {         List<user> Users = new arraylist<user> ();          Xmlresourceparser xrparser = Activity.getresources (). GETXML (R.xml.user);          try{             while (Xrparser.geteventtype ()! = Xm lresourceparser.end_document) {                 if (Xrparser.geteventtype () = = Xmlresourceparser.start_tag) {                     String Tagnam E = Xrparser.getname ();                      if ("User". Equals (TagName)) {   &nbsp ;                     User user = new user ();                          User.setname (Xrparser.getattribu Tevalue (NULL, "name"));                          User.setage (xRparser.getattributevalue (NULL, "age"));                          User.setpassword (Xrparser.gettex T ());                          users.add (user);                     }            &NB Sp    }                  xrparser.next ();             };         }catch (Exception e) {        }        &N Bsp return users;         }   Specific steps: 1. Through resource, get the XML file data, instantiate a XmlResourceParser2. Judging by Xmlresourceparser events, parsing xml  is simpler than sax (personally).

Android XML parsing

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.