Explain how Android uses Sax to parse XML files

There are many ways to parse XML, and the most familiar one may be DOM parsing.

DOM (file object model) parsing: the parser reads the entire document, then constructs a memory resident tree structure, and then the code can operate the tree structure according to the DOM interface.

Advantages: the whole document is read into memory and easy to operate: it supports multiple functions such as modification, deletion, reproduction and arrangement.

Disadvantages: reading the whole document into memory preserves too many unnecessary nodes and wastes memory and space.

Usage: once the document is read in, you need to operate the document many times, and when the hardware resources are sufficient (memory, CPU).

In order to solve the problems of DOM parsing, Sax parsing appears. Its features are:

Advantages: there is no need to transfer in the whole document and occupy less resources. Especially in embedded environments, such as Android, Sax parsing is highly recommended.

Disadvantages: unlike DOM parsing, the document resides in memory for a long time, and the data is not persistent. If the data is not saved after the event, the data will be lost.

Usage: the machine has performance limitations.

Sax parses XML documents in event driven mode. What is event driven mode? It transforms the XML document into a series of events, which are handled by a separate event handler.

Event driven processing mode mainly works based on event source and event processor (or listener). An object that can generate events is called an event source, and an object that can respond to events is called an event handler.

In the Sax interface, the event source is org xml. The xmlreader in the Sax package starts parsing XML documents through the parse () method and generates events according to the contents of the documents. The event handler is org xml. The four interfaces in Sax package are contenthandler, dtdhandler, errorhandler, and entityresolver. They handle different types of events generated by the event source during the parsing process (where dtdhandler is used to parse the document DTD). Details are shown in the following table:

Among the above four interfaces, the most important is the contenthandler interface. The following is a description of this interface method:

Here are the methods in xmlreader.

Steps for Sax to implement entity resolution:

Using Sax in Android is traceable. You can easily find the tag in XML and get the desired content according to the following methods. The specific implementation steps are as follows:

(1) Step 1: create a new factory class saxparserfactory with the following code:

(2) Step 2: let the factory class generate a Sax parsing class saxparser. The code is as follows:

(3) Step 3: get an xmlreader instance from saxpsrser. The code is as follows:

(4) Step 4: register the handler written by yourself into xmlreader. Generally, the most important is contenthandler. The code is as follows:

(5) Step 5: after turning an XML document or resource into an InputStream stream that can be processed by Java, the parsing officially begins. The code is as follows:

Among the above steps, the most important and critical is the fourth step, the implementation of handler.

The following is an example of RSS parsing to illustrate the implementation of handler:

First, we see an RSS XML document and realize local parsing. The new RSS document is as follows:

After it is built, we name it RSSXml XML and put it in the root directory of the project:

Then we can create two entity classes:

1. Rssfeed, corresponding to the complete XML document;

2. Rssitem, corresponding to the information in the item tag.

In this way, when parsing XML, we can put the parsed information into the entity class, and then directly operate the entity class. The code is given below:

RSSFeed. java

RSSItem. java

The following is the most important place. Create your own contenthandler Look at the following code:

RSSHandler. java

According to the above code analysis, the following steps are generally required to implement a contenthandler:

1. Declare a class that inherits defaulthandler. Defaulthandler is a base class, which simply implements a contenthandler. We just need to rewrite the methods inside.

2. Rewrite startdocument () and enddocument (). Generally, some initial wages before formal parsing are put in startdocument () and the finished work is put in enddocument ().

3. Rewrite startelement(), and the XML parser will call this function when it encounters a tag in XML. Often in this function, some data is operated through localname judgment.

4. Override the characters () method, which is a callback method. After the parser executes startelement (), it will execute this method after parsing the content of the node, and the parameter ch [] is the content of the node. In this example, we judge the content of the current tag according to the different currentstate, and put it into the appropriate entity class.

5. Rewrite the endelement () method, which corresponds to startelement (). After parsing a tag node, execute this method. In another example, if parsing an item ends, add rssiiem to rssfeed.

Finally, we implement an activity to show the parsing results:

Show the following operation results:

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
分享
二维码
< <上一篇
下一篇>>