Methods for parsing XML files in the Android system _java

Source: Internet
Author: User

Objective
when learning the framework layer of Android, Android uses Xmlpullparser to parse the source of XML files. So here's a brief introduction to the use of xmlpullparser.

Xml
XML (extensible Markup Language) is the Chinese name for Extensible Markup Language. Marking refers to the computer can understand the information symbols, through such a mark, the computer can deal with a variety of information and other articles.
use
XML is designed to transmit and carry data without displaying or presenting data, and the HTML language uses performance data, so the focus of XML usage is to explain what the data is, and to carry data information.

    • Rich files-Customizing file descriptions and making them richer
    • Metadata--Describes other files or network information
    • Configuration Document--describes parameters for software settings

Structure
each XML document starts with an XML preamble, and the first line in the preceding code is the XML preamble, <?xml version= "1.0"?>. This line of code tells the parser or browser that the file should be parsed according to the XML rules. However, the name of the root element is defined by the document type definition or XML outline.

xmlpullparser
    Pull parsing xml is an event-driven way to parse an XML file, and when pull begins parsing, We can get the current resolution event type through the Geteventtype () method and get the next parse event type through the next () method. The pull parser provides four types of event resolution for Start_document (start document), End_document (end document), Start_tag (start tag), End_tag (end tag). When you are in an element, you can call the Getattributevalue () method to get the value of the property, or you can get the text value of this node through the Nexttext () method. The following is an example to parse. The
XML sample file
    The XML sample file code that needs to be parsed is as follows:

  <?xml version= "1.0" encoding= "UTF-8"?> 
  <colleagues> 
    <colleague id= "1" > 
      <name> Mice </name> 
      <age>24</age> 
      <sex>boy</sex> 
    </colleague> 
    < Colleague Id= "2" > 
      <name> lulu </name> 
      <age>28</age> 
      <sex>girl</sex > 
    </colleague> 
    <colleague id= "3" > 
      <name> Chen Shan </name> 
      <age>26 </age> 
      <sex>boy</sex> 
    </colleague> 
  </colleagues> 

Xmlpullparser Parser

  Package Com.example.shakedemo; 
  Import Java.io.File; 
  Import java.io.FileNotFoundException; 
  Import Java.io.FileReader; 
  Import java.io.IOException; 
  Import java.util.ArrayList; 
  Import java.util.List; 
  Import Org.xmlpull.v1.XmlPullParser; 
  Import org.xmlpull.v1.XmlPullParserException; 
  Import Org.xmlpull.v1.XmlPullParserFactory; Import Android. 
  R.xml; 
  Import Android.util.Log; 
   
  Import android.util.Xml; 
      public class Xmlpullparserhelper {public static list<colleague> getcolleagues (String xmlfilepath) { 
      List<colleague> colleagues = new arraylist<colleague> (); 
      FileReader xmlReader = null; 
      try {xmlReader = new FileReader (new File (Xmlfilepath)); 
        catch (FileNotFoundException e) {log.e ("Wzy", "couldn ' t find XML file" + Xmlfilepath); 
      Return colleagues; try {//mode 1: Get parser object Xmlpullparser using the Android.util.Xml class provided by Android ParsER = Xml.newpullparser (); Mode 2: Use factory class Xmlpullparserfactory//Xmlpullparserfactory pullfactory =//Xmlpullparserfactory.newinst 
        Ance (); 
   
        Xmlpullparser parser = Pullfactory.newpullparser (); 
   
        Set the file input stream parser.setinput (XmlReader); 
   
        Gets the current event type int eventtype = Parser.geteventtype (); 
   
        Colleague colleague = null; while (EventType!= xmlpullparser.end_document) {switch (eventtype) {case XMLPULLPARSER.START_DOCU 
          Ment:break; Case Xmlpullparser.start_tag:/** * Through the getname to determine which label to read, and then through the nexttext to get the text node value, * or pass 
            Over Getattributevalue (i) Get the attribute node value/String name = Parser.getname (); 
              if ("Colleague". Equals (name)) {colleague = new colleague (); 
            Colleague.setid (Integer.parseint (Parser.getattributevalue (NULL, "id")); else if ("name". Equals (name) {if (colleague!= null) {Colleague.setname (Parser.nexttext ()); ' Else if ' (' Age '. Equals (name) {if (colleague!= null) {COLLEAGUE.S 
              Etage (Integer.parseint (Parser.nexttext ())); } else if ("Sex". Equals (name)) {if (colleague!= null) {Colleague.setsex ( 
              Parser.nexttext ()); 
          }} break; 
              Case XmlPullParser.END_TAG:if ("colleague". Equals (Parser.getname ()) && colleague!= null) { 
              Colleagues.add (colleague); 
            colleague = null; 
          } break; 
        } EventType = Parser.next (); 
      } xmlreader.close (); 
      catch (Xmlpullparserexception e) {/Do nothing} catch (IOException e) { 
    Return colleagues;} 
  } 

 

Among them, the definition of colleague class is relatively simple, the code is as follows:

 Package Com.example.shakedemo; 
   
  Public class Colleague { 
    private int id; 
    private int age; 
    private String name; 
    Private String sex; 
   
    public int getId () {return 
      ID; 
    } 
   
    public void setId (int id) { 
      this.id = ID; 
    } 
   
    public int getage () {return age 
      ; 
    } 
   
    public void Setage (int age) { 
      this.age = age; 
    } 
   
    Public String GetName () {return 
      name; 
    } 
   
    public void SetName (String name) { 
      this.name = name; 
    } 
   
    Public String Getsex () {return 
      sex; 
    } 
   
    public void Setsex (String sex) { 
      this.sex = sex; 
    } 
   
    @Override public 
    String toString () {return 
      ' ID is ' + ID + ', name is ' + Name + ', Sex is ' + Sex; 
    } 
   
  } 

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.