Android uses Sax to add, delete, modify and check XML

preface

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.

This article will give you a detailed introduction to Android's use of Sax to add, delete, modify and query XML, and share it for your reference and learning value. I won't say more below. Let's take a look at the detailed introduction together.

1. General

Sax is an event driven XML parsing method in. To put it bluntly, it is to inform the parsing result by copying a default class. Sax doesn't load the whole XML into memory like DOM, but it parses it tag by tag like IO stream.

In short, it is to scan the document in sequence. When it is scanned to the beginning and end of the document, the beginning and end of the element, the end of the document, etc., the event processing function is notified, the event processing function takes corresponding actions, and then continue the same scanning until the end of the document.

For the convenience of explanation, an XML is agreed as follows:

2. Basic reading (check)

The code is as follows

Resolve to < person > < / person > callback: startelement. The parameter in the tag is attributes attributes. A for loop can traverse and read.

Characters, call back when parsing the content of the tag. Then, in the above example, parse < person > < / person >, call back the startelement, and then do not call back this method. Because the content is not text, but contains the tag, so when parsing to its sub tag: < name > Zhangsan < / name >, call back the startelement first, and then call back characters. Let me tell you, There is text in this label! The parameters are described as follows:

Using the above code, the partial log obtained is as follows:

Startdocument, start parsing XML

Parse to the beginning of the first tag: < persons >

Then it parses the content??? characters? According to my analysis above, there is no text content in the < persons > tag and there should be no callback. In fact, the callback here is a newline character. The ASCII code is printed in log, and 10 is line feed. Then, there is a tab.

Then there is < person > in < persons >, which has three parameters: ID, key, type, balabalabala...

3. Save

Sax is a little troublesome to save. Specifically, the use of XmlSerializer.

Initialize an XmlSerializer:

Declare the beginning and end of the document:

Start and end of label, and write content:

Actual combat:

Suppose we need to build the following XML:

First, you have to define a bean class, person:

Then open: the last stringwriter is the data you want. Note that some line breaks and tabs.

The initialization of XmlSerializer needs to pass in a write object. You can pass in a FileWrite and write it to the file:

4. Addition and deletion

To add and delete, you need to map the XML into a pile of beans, then add and delete beans, and then save them.

summary

The above is the whole content of this article. I hope the content of this article has a certain reference value for your study or work. If you have any questions, you can leave a message. Thank you for your support for 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
分享
二维码
< <上一篇
下一篇>>