Android creation and parsing XML (6) -- Comparison and use

Source: Internet
Author: User

We have introduced four ways to create and parse XML:

0. Create and parse XML for Android (1) -- Overview

1. Create and parse XML in Android (2) -- DOM Mode

2. Create and parse XML for Android (3) -- using the sax Method

3. Create and parse XML for Android (4)-Pull Mode

4. Create and parse XML in Android (5) -- dom4j Mode

From the perspective of processing methods, some adopt the java standard method for processing XML, some adopt the XML processing method after third-party improvement; from the event perspective, some are based on DOM tree nodes, some event-based processing

Why are there so many ways to create and parse XML? What are the characteristics of the four processing methods? What application scenarios are they more suitable?

I. general XML standards

XMLThe world is huge and growing. There are a lot of different standards and technologies that affect each other in a complicated way.

XML is becoming more and more powerful and rapidly evolving. It has proved itself to be a very valuable technology, but it may also be a daunting technology, considering the changing parts under the word "XML", it is difficult for new users to determine which are the most important aspects of XML, and it is difficult for users to track new things and changes in this field.

Standards, there are various forms, and there are often a variety of standards competing in the same field, here the standard is defined: standards adopted by different suppliers in large or influential and independently recommended by supplier organizations.

1) XML 1.0, W3C recommendation standards, derived from the backbone of the XML technology tree. Based on the Unicode [Unicode Consortium Technical Report and ISO standards], it defines strict rules for text formats and the DTD (Document Type Definition, document type definition) Verification language. The current version of this specification (version 2nd) contains previous revisions to the specification. It is translated into multiple languages, although the English version is the only standard version, that is, only this version is considered to have the standard effect.

2) XML 1.1Is under development. It is the first revision of the definition of an XML document with a good structure changed. The main change is to revise the processing of Characters in the XML specification to make it more natural to adapt to the changes in the Unicode specification, and reference the character model for the World Wide Web 1.0) [under development], it provides character normalization for different Unicode versions. XML 1.1 also adds the row end character list, and the new nel is used to indicate the row end (EoL) in the IBM mainframe system ). This change is controversial, and some people think that the limited benefits to mainframe users are not worthy of this fundamental change. There are also some other arguments, because some reviewers find that all the modifications are too secure and there will be no possible interoperability issues in XML version changes.

XML is based on Standard Generalized Markup Language (SGML), which is defined by ISO 8879: 1986 [ISO standard. It greatly simplifies SGML, including some adjustments to make it more suitable for the Web environment.

Ii. XML Processing Method

Most XML-related Java APIs are fully supported on Android, and powerful mobile applications can be created on Android, XML parsing Technology on Android includes three types of Dom, sax, and pull.

1) Dom parsing Technology

Dom (Document Object Model, Document Object Model). Android fully supports Dom parsing. Using DOM objects, you can read, search, modify, add, and delete XML documents.

When using Dom to operate an XML file, you must first parse the file, divide the file into independent elements, attributes, and annotations, and then represent the XML file in the memory in the form of a node tree, you can access the document content through the node tree and modify the document as needed-this is how Dom works. When Dom is implemented, a set of interfaces are defined for the parsing of the XML document. The parser reads the entire document and constructs a tree structure with resident memory, so that the code can use the DOM interface to operate the entire tree structure.

Dom parsing process:

2) sax parsing Technology

SAX (Simple API for XML, simple XML application interface) is a common event-based XML document parsing standard. It uses events as the mode for parsing XML files. It converts XML files into a series of events, which are determined by different event processors. Sax is an XML parser with high resolution speed and low memory usage. It is very suitable for Android and other mobile devices. It uses event-driven XML parsing files, that is, it does not need to parse the entire document. In the process of parsing the document in the content order, Sax will determine whether the currently read characters are a part of the legal XML syntax, if yes, the event is triggered.

Sax parsing process:

3) Pull parsing Technology

Android API also provides android. util. XML class, you can also Parse XML files. Using methods similar to Sax also requires writing handler to process XML parsing, but it is easier to use than sax. It allows your application code to get events from the parser, which is the opposite of the event automatically pushed into the handler by the SAX Parser. The pull parser runs in a similar way as the SAX Parser. It provides similar ide events, such as the start and end elements, using parser. next () can enter the next element and trigger the corresponding event. The event is sent as a numerical code, so you can use a switch to process the event you are interested in. When parsing an element, call parser. nexttext () to obtain the value of a text node.

Pull parsing process:

Iii. xml Performance Comparison

  • Processing time

  • Memory usage

Iv. Use Cases

The Dom parser parses an XML document into a tree model and puts it into the memory for parsing. Then, operations on the document are completed on the tree model. The document tree in the memory will be several times the actual size of the document. The advantage of this is that the structure is cleared and the operation is convenient, and the trouble is that the system resources are greatly consumed.

The SAX Parser overcomes the disadvantages of Dom. analysis can start immediately, instead of waiting for all the data to be processed. Moreover, because the application only checks data when reading data, it does not need to store the data in the memory, which is a huge advantage for large documents. In fact, the application does not even have to parse the entire document; it can stop parsing when a condition is met.

The XML parsing technology you choose depends on the following factors:

(1) Purpose of the application: If you plan to make changes to the data and output it as XML, the Dom is an appropriate choice in most cases. It doesn't mean that you can't change data by using Sax and pull, but the process is much more complicated, because you must copy the data instead of making changes to the data itself.

(2) Data capacity: for large files, Sax and pull are better choices.

(3) data usage: If only a small part of the data is used, it may be better to use SAX and pull to extract the part of the data to the application. On the other hand, if you know that you will reference a large amount of information that has been processed in the future, it may not be an appropriate choice for Sax and pull.

(4) speed requirement: Sax and pull implementations are usually faster than dom implementations.

(5) add nodes: to dynamically add nodes to XML, Dom is recommended.

(6) When both Sax and pull can be used, we recommend that you use pull for parsing.

V. Summary

For Android mobile devices, because the device resources are precious and the memory is limited, we need to select the appropriate technology to parse XML, which is conducive to improving the access speed.

(1) When Dom processes XML files, it parses the XML files into a tree structure and puts them in the memory for processing. When the XML file is small, we can select Dom because it is simple and intuitive.

(2) sax uses events as the mode for parsing XML files. It converts XML files into a series of events, which are determined by different event processors. When the XML file is large, it is reasonable to select the sax technology. Although the amount of code is large, it does not need to load all XML files into the memory. This is more effective for limited Android memory, and Android provides a traditional method of using Sax and a convenient sax package. Use the Android. util. xml class.

(3) Pull parsing does not end listening elements like the sax parsing, but completes most of the processing at the beginning. This facilitates reading XML files early and greatly reduces the parsing time. This optimization is especially important for mobile devices with slow connection speeds. When the XML document is large but only a part of the document is required, the pull parser is more effective.

Reference recommendations:

XML standard Overview (IBM)

XML parsing Technology for Android

XML parsing scheme selection in Android system

Analysis of XML parsing Technology on Android platform

W3C

W3C schools)

W3C schools (Chinese)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.