This article describes the Android programming method of parsing XML files. Share it for your reference, as follows:
Preface
When learning Android's Framework layer source code, Android uses XmlPullParser extensively to parse the source code of xml files. Therefore, here we will also introduce the use of XmlPullParser.
XML
XML (eXtensible Markup Language) is called Extensible Markup Language in Chinese. Marking refers to information symbols that a computer can understand. Through this marking, articles containing various information can be processed between computers.
use
XML design uses the transmission and carrying data information, without expressing or displaying data, HTML language uses expressing data, so the focus of XML uses it to explain what data is and carry data information.
Rich files——Customize the file description and make it richer
Metadata——Describe other documents or online information
Configuration Documentation——Describe the parameters that are set by the software
structure
Each XML document starts with an XML preamble. The first line in the previous code is the XML preamble, <?xml version="1.0"?>. This line of code will tell the parser or browser that the file should be parsed according to XML rules. However, the name of the root element is defined by the document type or XML schema.
XmlPullParser
PULL parsing xml is based on an event-driven method to parse XML files. When pull starts parsing, we can first obtain the current parsed event type through the getEventType() method, and obtain the next parsed event type through the next() method. The PULL parser provides four event resolution types: START_DOCUMENT, END_DOCUMENT, START_TAG (start tag), and END_TAG (end tag). When it is in a certain element, you can call the getAttributeValue() method to get the value of the attribute, or you can use the nextText() method to get the text value of this node. The following is an example to analyze.
xml sample file
The code of the xml sample file that needs to be parsed is as follows:
<?xml version="1.0" encoding="UTF-8"?> <colleagues> <colleague > <name>Mouse</name> <age>24</age> <sex>boy</sex> </colleague> <colleague > <name>Lulu</name> <age>28</age> <sex>girl</sex> </colleague> <colleague > <name>Chen Shan</name> <age>26</age> <sex>boy</sex> </colleague> </colleagues>
XmlPullParser parser
package ; import ; import ; import ; import ; import ; import ; import .; import .; import .; import ; import ; import ; 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) { ("wzy", "Couldn't find xml file " + xmlFilePath); return colleagues; } try { // Method 1: Use the class provided by Android to obtain parser object XmlPullParser parser = (); // Method 2: Use factory class XmlPullParserFactory // XmlPullParserFactory pullFactory = // (); // XmlPullParser parser = (); // Set file input stream (xmlReader); // Get the current event type int eventType = (); Colleague colleague = null; while (eventType != XmlPullParser.END_DOCUMENT) { switch (eventType) { case XmlPullParser.START_DOCUMENT: break; case XmlPullParser.START_TAG: /** * Use getName to determine which tag is read, and then use nextText to get the text node value. * Or get the attribute node value through getAttributeValue(i) */ String name = (); if ("colleague".equals(name)) { colleague = new Colleague(); (((null, "id"))); } else if ("name".equals(name)) { if (colleague != null) { (()); } } else if ("age".equals(name)) { if (colleague != null) { ((())); } } else if ("sex".equals(name)) { if (colleague != null) { (()); } } break; case XmlPullParser.END_TAG: if ("colleague".equals(()) && colleague != null) { (colleague); colleague = null; } break; } eventType = (); } (); } 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 ; 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) { = id; } public int getAge() { return age; } public void setAge(int age) { = age; } public String getName() { return name; } public void setName(String name) { = name; } public String getSex() { return sex; } public void setSex(String sex) { = sex; } @Override public String toString() { return "ID is " + id + ", Name is " + name + ", Sex is " + sex; } }
PS: Here are a few online tools for your reference:
OnlineXML/JSON mutual conversion tool:
http://tools./code/xmljson
Online formattingXML/Online compressionXML:
http://tools./code/xmlformat
XMLOnline compression/formatting tools:
http://tools./code/xml_format_compress
XMLCode online formatting and beautification tool:
http://tools./code/xmlcodeformat
For more information about Android related content, please check out the topic of this site:Summary of Android operating XML data skills》、《Android programming activity operation skills summary》、《Android resource operation skills summary》、《Android file operation skills summary》、《Android development introduction and advanced tutorial》、《Android View View Tips Summary"and"Android control usage summary》
I hope this article will be helpful to everyone's Android programming design.