Foreword
When learning Android's Framework layer source code, Android uses XMLPULLPARSER to resolve the source code of the XML file. Therefore, here is also the use of XMLPullParser.
XML
XML (Extensible Markup Language) Chinese name is a scalable tag language. The mark refers to the information symbol that the computer can understand. Through this mark, articles that contain various information can be processed between computers.
use
The XML design uses transmission and carrying data information. It does not use performance or display data. The HTML language uses performance data. Therefore, the focus of XML use is what it explains the data and the data information.
structure
Each XML document starts with XML preface. The first line in the previous code is the XML preface, <? XML Version = "1.0"?>. This line of code tells the parser or browser. This file should be parsed in accordance with the XML rules. However, the name of the root element is defined by the document type or the XML outline.
Xmlpullparser
PULL parsing XML is based on event -driven XML files. When the PULL starts parsing, we can first obtain the current analytical event type through the GeteventType () method, and obtain the next analysis event type through the Next () method. The PULL parser provides four event analysis types: Start_Document (starting document), END_DOCUMENT When you are in a certain element, you can call the gettattributevalue () method to obtain the value of the attribute. You can also obtain the text value of the node through the NextText () method. Let's analyze it through an example.
XML example 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> Sedu </name> <Age> 24 </Age> <sex> Boy </ Sex> </colorAgue> <colleague ID = "2"> <name> Lulu </name> <te> 28 </Age> <sex> GIRL </sex> </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.filenotFoundexception; Import Java.Filerereader; Import ION; Import Java.util.arrayList; Import Java.util.list; Import Org.xmlpull.v1.xmlpullparser; Import org.xmlpull.v1.xmlPullparserexception; Import org.xmlpull.v1.xmlparserFactory; Import ml; Import Android.util.log; Import android.util.xml; Public Class XMLPULLLSERHELPER {PUBLIC Static list <colleague> getColleagues (String XMLFILEPATH) {list <colleague> COLLEAGUES = New ArrayList <); null; Try {xmlreader = New FileRereader (New File (XMLFILEPATH)); e) {Log.e ("WZY", "COULDN'T Find XML FILE" + XMLFILEPATH); Return Colleagues;} Try {// Method 1: Use Android.util.xml provided by Android Arser Parser = Xml.newpullparser (); // Method 2: Use the factory class XMLPULLPARSERFACTORY // xmlpullparserFactory Pullfactory = // xmlpullparserFactory.newinstance (); // xmlpullparpar serrate = publicFactory.newpullparser (); // Set the file input stream Parser.Setinput (xmlReader ); // Get the current event type int eventType = Parser.GeteventType (); ColleaGue ColleaGue = Null; While (EventType! = Xmlpullparser.eCument) {Switch PE) {case xmlpullparser.start_document: break; case xmlpullparser.start_tag: / ** * Detect which label you read through GetName, and then obtain the text node value through NextText, or obtain the attribute node value through gettattributevalue (i) */ string name = Parser.getName (); if ("Colleague" .equals (Na me )) {Colleague = New ColleaGue (); ColleaGue.setID (Integer.parseint (Parser.getattributevalue (NULL, "ID"));} Else if ("name" .quals (names)) {if (colorague! = null) {colorAgue.setname (Parser.nextText ());}} Else if ("Age" .equals (name)) {if (colorAgue! = NULL) {ColleAgue.Setage (Integer.parseint (Parser.nextText () );}} Else if ("sex" .equals (name)) {if (colleag! = Null) {colorAgue.setsex (Parser.nextText ());} Break; case; : if ("Colleague ".quals (Parser.getName ()) && COLLEAGUE! = Null) {Colleagues.add (Colleague); Colleague = NULL;} EventType = Parser.next ();} lreamer.close ();} Catch ( Xmlpullparserexception E) {// Do Nothing} Catch (IOEXCEPTION E) {// Do Nothing} Return Colleagues;}}Among them, the definition of the ColleAgue class is relatively simple, and the code is as follows:
package com.example.shakedemo; Public Class ColleaGue {Private into; Private String name; Private String Sex; ) {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 () {) d 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;}}