XML parsing in Android-Overview

Source: Internet
Author: User

XML is widely used in various development scenarios, and Android is no exception. As an important role in carrying data, reading and writing XML has become an important skill in Android development.

In Android, common XML parsers are Dom parsers, Sax parsers, and pull parsers, which are described in detail below.

The first method -- DOM Parser:

Dom is a collection of nodes or information fragments based on a tree structure. It allows developers to use Dom APIs to traverse XML trees and retrieve required data. To analyze this structure, you usually need to load the entire document and construct a tree structure before you can retrieve and update node information.

Because the Dom is stored in the memory in a tree structure, the retrieval and update efficiency is higher. However, parsing and loading a very large document will consume a lot of resources. If the content of the XML file is small, Dom is also feasible.


How Dom works:

To use Dom to operate an XML file, you must first parse the file, which is divided into independent elements, attributes, and annotations. Then, the XML file is represented in the memory as the number of nodes, you can access the document content through the node tree and modify the document as needed;


Common Dom interfaces and classes:

Document: this interface defines a series of methods for analyzing and creating Dom documents. It is the root of the Document Tree and serves as the basis for Dom operations.

Element: this interface inherits the node interface and provides methods to obtain and modify the names and attributes of XML elements.

Node: this interface provides methods to process and obtain the values of nodes and subnodes.

Nodelist: provides methods to obtain the number of nodes and the current node. In this way, you can access each node iteratively.

Domparser: this class is the DOM parser class in Apache xerces, which can directly Parse XML files.


Dom parsing process:



Method 2-The SAX Parser:

The Simple API for XML parser is an event-based parser. It focuses on the event processing mode and mainly focuses on Event sources and event processors.

The advantage of the SAX Parser is that the parsing speed is fast and the memory usage is small. It is ideal for Android mobile devices. But I personally think it is complicated.


How sax works:

The working principle of Sax is simply to scan documents in sequence. When a document is scanned to start and end, elements start and end, and documents) the event handler function is notified when the event ends. The event handler function performs the corresponding action and continues the same scan until the document ends.

In the sax interface, the event source is xmlreader In the org. xml. Sax package. It parses the XML document through the Parser () method and generates events.

The event processor is the contenthander, dtdhander, errorhandler, and entityresolver interfaces in the org. xml. Sax package.

Xmlreader connects to contenthander, dtdhander, errorhandler, and entityresolver through the corresponding event processor registration method setxxxx.

Common sax interfaces and classes:

Attrbutes: used to obtain the number, name, and value of an attribute.

Contenthandler: defines the events associated with the document itself (for example, start and end tags ). Most applications register these events.

Dtdhandler: defines the events associated with the DTD. It does not define enough events to completely report the DTD. To analyze the DTD syntax, use the optional declhandler.

Declhandler is an extension of sax. Not all syntax analyzers support it.

Entityresolver: defines the events associated with the loaded object. Only a few applications register these events.

Errorhandler: defines error events. Many applications register these events to report errors in their own way.

Defaulthandler: it provides these default implementations connected to Li. In most cases, it is easier to extend defaulthandler for the application and overwrite the relevant methods than to directly implement an interface.

See the table below:

We need xmlreader and defaulthandler to parse XML.The following is the parsing process of sax:

The third method -- pull Parser:

The Running Method of the pull parser is similar to that of the SAX Parser, and is based on the event mode. The difference is that in the pull parsing process, numbers are returned, and we need to obtain the generated events and perform corresponding operations, instead of using a processor to trigger an event like sax, execute our code.

Start_document;

End_document is returned when the end of the read to XML;

Start_tag is returned when the start tag is read to XML.

The end_tag is returned when the end tag of the XML file is read.

Return text

The pull parser is compact, lightweight, fast in resolution, and easy to use. It is very suitable for Android mobile devices. The Android system also uses the pull Parser for parsing Various XML files, android officially recommends developers to use the pull parsing technology. Pull Parsing is an open-source technology developed by a third party. It can also be used in javase development.


Working principle of pull:

XML pull provides the start and end elements. When an element starts, we can call parser. nexttext to extract all characters from the XML document. An enddocument event is automatically generated when the end of a document is interpreted.

Common XML pull interfaces and classes:

Xmlpullparser: the XML pull parser is an interface that provides the definition parsing function in xmlpull vlap1.

Xmlserializer: it is an interface that defines the sequence of XML Information sets.

Xmlpullparserfactory: this class is used to create an XML pull parser in xmpull V1 API.

Xmlpullparserexception: throws errors related to a single XML pull parser.

The parsing process of pull is as follows:

Instance:

XML parsing in Android-instance

References:

Http://blog.csdn.net/liuhe688/article/details/6415593

Http://www.2cto.com/kf/201202/121173.html

Related Article

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.