Detailed explanation of XML file parsing method for Android programming (with source code)

This paper describes the XML file parsing method of Android programming. Share with you for your reference, as follows:

In Android development, it is often used to parse XML files. There are three common ways to parse XML: sax, pull and DOM. Recently, I made an Android version of CSDN reader, using two of them (Sax, pull). Today, I summarize these three ways of Android parsing XML.

The XML example parsed today (channels. XML) is as follows:

1、 Parsing using Sax

Basics:

Parsing in this way is an event driven API, which has two parts: parser and event processor. The parser is the xmlreader interface, which is responsible for reading XML documents and sending events to the event processor (also the event source). The event processor contenthandler interface is responsible for responding to sent events and processing XML documents.

The following are the common methods of the contenthandler interface

public abstract void characters (char[] ch,int start,int length)

This method is used to receive the character block notification. The parser reports the character data block through this method. In order to improve the parsing efficiency, the parser puts all the read strings into a character array (CH) and passes them to the character method as parameters. If you want to obtain the character data read in this event, you need to use the start and length attributes.

Public abstract void startdocument() receives notification of document start

Public abstract void enddocument() receives notification of the end of the document

Public abstract void startelement (string URI, string localname, string QName, attributes attributes) receives the label of the beginning of the document

Public abstract void endelement (string URI, string QName) receives the tag of the end of the document

In general use, in order to simplify development, a defaulthandler class is provided in org.xml.sax.helpers, which implements the contenthandler method. We just want to inherit the defaulthandler method.

In addition, the Sax parser provides a factory class: saxparserfactory. The parsing class of Sax is saxparser, and its parser method can be called for parsing.

After reading some basics, start coding (core code, download the code in the attachment)

From the second part of the code, you can see the steps of parsing XML using sax:

1. Instantiate a factory saxparserfactory

2. Instantiate saxpraser object, @ r_ 419_ 1740@Reader Parser

3. Instantiate handler, processor

4. The parser registers an event

4. Read file stream

5. Parse file

2、 Use pull mode to parse

Basics:

In the Android system, many resource files are in XML format. In the Android system, pul parser is used to parse these XML. It is the same as sax parsing (personally, it is simpler than sax). It is also event driven. When pull parser starts parsing, we can call its next () method, To get the next parsing event (i.e. start document, end document, start tag, end tag). When in an element, you can call the getattribute() method of xmlpullparser to get the value of the attribute, or you can call its nexttext() to get the value of this node.

In fact, the above description is a description of the whole parsing step. Look at the code

3、 Resolve using DOM

Basics:

Finally, let's take a look at the DOM parsing method. This method has not been used before (it is common in J2EE development and has not been done in this regard). In the process of DOM parsing, first read all DOM files into memory, and then use DOM API to traverse all data and retrieve the desired data. This method is obviously a memory consuming method, For mobile devices such as mobile phones, the memory is very limited, so this method is not recommended for large XML files, but DOM also has its advantages. It is more intuitive and simpler than Sax in some aspects. When the XML document is small, you can also consider using dom.

The core code of DOM parsing is as follows:

Summarize the steps of DOM parsing (similar to sax)

1. Call the documentbuilderfactory. Newinstance () method to get the DOM parser factory class instance.

2. Call the newdocumentbuilder () method of the parser factory instance class to get the DOM parser object

3. Call the parse () method of the DOM parser object to parse the XML document to get the document object representing the whole document.

4、 Summary

In addition to the above three methods, there are many ways to parse XML, such as Dom4j, JDOM and so on. However, there are two basic parsing methods, one is event driven (representing sax) and the other is based on document structure (representing DOM). The others are just different in grammar.

Attached (screenshot of sample operation in this article):

Click here to download the complete example code.

I hope this article will help you in Android programming.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>