Explain in detail the method of parsing XML in Android

XML is widely used in all kinds of development, and Android is no exception. As an important role in carrying data, how to read and write XML has become an important skill in Android development. Today, I'd like to introduce several common XML parsing and creation methods on the Android platform.

In Android, the common XML parsers are Sax parser, DOM parser and pull parser. Next, I will introduce them in detail.

Sax parser:

Sax (simple API for XML) parser is an event based parser. Its core is the event processing mode, which mainly works around the event source and event processor. When the event source generates an event, the event handler calls the corresponding processing method, and an event can be processed. When the event source calls a specific method in the event processor, it should also pass the state information of the corresponding event to the event processor, so that the event processor can determine its own behavior according to the provided event information. Sax parser has the advantages of fast parsing speed and less memory. Ideal for Android mobile devices.

DOM parser:

DOM is a collection of nodes or information fragments based on tree structure, which allows developers to use DOM API to traverse XML tree and retrieve required data. Analyzing this structure usually requires loading the whole document and constructing a tree structure, and then the node information can be retrieved and updated. Because DOM is stored in memory in a tree structure, retrieval and update efficiency will be higher. However, for very large documents, parsing and loading the whole document will consume resources.

Pull parser:

The pull parser runs in an event based mode similar to Sax. The difference is that in the process of pull parsing, we need to get the generated events and then do the corresponding operations. Unlike Sax, the processor triggers an event to execute our code. The pull parser is small and light, fast and easy to use. It is very suitable for Android mobile devices. The pull parser is also used to parse various XML in the Android system.

The above three parsers are very practical parsers, which I will introduce one by one. We will use these three Parsing Techniques to accomplish a common task.

We will create a new project with the following structure:

I will place an XML document books.xml in the assets directory of the project, with the following contents:

Then we use the above three Parsing Techniques to parse the document and get a list < book > object. Let's take a look at the code of book.java:

Finally, we need to generate a new XML document from the data in the collection object, as shown in the figure below:

The generated XML structure is slightly different from the original document in the following format:

Next, it's time to introduce the operation process. First, we define a bookparser interface for the parser. Each type of parser needs to implement this interface. The code of bookparser.java is as follows:

Well, we should implement the interface one by one and complete our parsing process.

Use Sax parser: the code of saxbookparser.java is as follows:

In the code, we defined our own event processing logic and rewritten several important event methods of defaulthandler. Let me focus on the relevant knowledge of defaulthandler. Defaulthandler is an event handler that can receive all events reported by the parser and process the data found. It implements entityresolver interface, dtdhandler interface, errorhandler interface and contenthandler interface. These interfaces represent different types of event handlers. Let's focus on the contenthandler interface. The structure is shown in the figure:

Finally, we need to call the Sax parser. This step is completed in mainactivity:

There are only two buttons on the interface, which are attached to you by the way:

Use DOM parser: dombookparser.java code is as follows:

Then change only one place in mainactivity:

The execution result is the same.

Use the pull parser: the pullbookparser.java code is as follows:

Then make the following changes to mainactivity:

And the other two execution results are the same.

These three parsers have their own advantages. Personally, I prefer the pull parser, because the Sax parser is too cumbersome to operate, and the DOM is not suitable for scenes with large documents and small memory. Only pull is light and flexible, fast, occupies small memory, and is very easy to use. Readers can also choose the corresponding parsing technology according to their preferences.

The above is the whole content of this article. I hope it will be helpful to your study, and I hope you can support programming tips.

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