When learning the source code of Android's framework layer, Android uses Xmlpullparser to parse the source code of the XML file. So, here is also the introduction of the use of Xmlpullparser.
XML XML (extensible Markup Language) Chinese name is Extensible Markup Language. tags refer to information symbols that can be understood by a computer, which can be used to process articles that contain various kinds of information between computers. The purpose of XML design is to transmit and carry data information, no performance or display of data, the HTML language uses performance data, so the focus of XML use is what it describes the data, and carry data information.
- Enrich files-Customize the file description and enrich it
- Metadata--Describe other files or network information
- Configuration document--parameters describing the software settings
Structure each XML document starts with the XML prologue, and in the preceding code the first line 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 the XML schema.
Xmlpullparser Pull parsing XML is an event-driven parsing of an XML file, and when pulling begins parsing, we can get the current parse 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 of parsing. The XML sample file needs to parse the XML sample file code 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: Use the Android.util.Xml class provided by Android to get parser object Xmlpullparser parser = Xml.newpullparser ();//Mode 2: Use factory class XMLPULLP arserfactory//xmlpullparserfactory pullfactory =//xmlpullparserfactory.newinstance ();//XmlPullParser parser = Pullfactory.newpullParser ();//Set file input stream Parser.setinput (xmlReader);//Get current event type int eventtype = Parser.geteventtype (); Colleague colleague = Null;while (eventtype! = xmlpullparser.end_document) {switch (eventtype) {case Xmlpullparser.start _document:break;case xmlpullparser.start_tag:/** * Read the tag by GetName and then get the value of the text node by Nexttext, or by Getattributevalue (i Gets 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.setage (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 not} catch (IOException e) {//Do Nothing}return colleagues;}}Among them, the definition of the 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 SE x;public int getId () {return ID;} public void setId (int id) {this.id = ID;} public int getage () {return age;} public void Setage (int.) {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;} @Overridepublic String toString () {return ' ID is ' + ID + ', Name is ' + name + ', sex is ' + Sex;}}
Have time, I will consider adding other XML file parsing Method!
Android parsing xml file