Android Learning XML Parsing Encyclopedia of Sax and Dom

Source: Internet
Author: User

with the learning of Android parsing XML became a question many friends faced. To learn how to parse XML, you should first understand what XML is.

XML, known as Extensible Markup Language (extensible Markup Language), is developed by the standard Universal Markup Language (Sgml:standard generalized Markup Language), allowing developers to customize tags, Effective separation of tags and content is possible.

Unlike HTML, XML is no longer focused on how data behaves, but more about how data is stored and transmitted. As a result, XML has evolved into a cross-platform data exchange format. By using XML, developers can exchange data between different platforms and different systems. In addition, you can use XML as a configuration file to save application state to an XML file without having to use a relational database.

Two common ways to parse XML are: DOM SAX
The difference :
The DOM reads the document into memory through a standard interface,
The advantage is that each element in the XML can be accessed randomly.
The disadvantage is that the entire DOM tree resides in memory, resulting in excessive overhead when it is read into the whole document at once.
SAX uses a time-driven mechanism to parse XML documents. Get XML information from developer-written event listeners.
The advantage is that the parsing method consumes little memory and is processed quickly.
The disadvantage is that sequential patterns are used to read the XML, so arbitrary elements in the document cannot be read randomly.
JAXP (Java API for XML) JAXP is a layer of abstraction built on DOM and sax that neither provides a new way to parse XML, nor any extensions to DOM and sax, but simply provides a factory pattern, Allows the application to switch between different XML parsers.

SAX parser

JAXP provides the following 2 sets of APIs for the SAX parser.

(1) the Creatxmlreader () static method of the XmlReader and Xmlreaderfactory:xmlreaderfactory engineering classes is used to create XmlReader.

(2) The Newsaxparser () instance method of the SAXParser and Saxparserfactory:saxparserfactory factory classes is used to create saxparser.

Both the XmlReader and SAXParser in the two sets of APIs are SAX parsers that define multiple parser () methods for parsing XML documents in a sax manner.

Among them, XmlReader provides the following 2 parser () Methods for parsing XML documents.

(1) void parse (InputSource input); Parse an XML document from the InputSource input source

(2) void Parse (String systemid); Parsing the XML document specified by the system URI

SAXParser provides the following 4 parser () Methods for parsing XML documents.

(1) void Parse (File F, DefaultHandler DH); Parse the XML document represented by the F file

(2) void Parse (InputSource is, DefaultHandler dh); Parse an XML document from the InputSource input source

(3) Void Parse (InputStream is, DefaultHandler dh); Parse an XML document from the InputStream input source

(4) Void parse (String uri, DefaultHandler dh); Parsing the XML document specified by the system URI

As you can see, in SAXParser's parser () method, the second argument is a DefaultHandler object, which is the listener that listens for Sax parsing events.

Listener for sax :
(1) ContentHandler: Strong XML document content handling events
(2) Dtdhandler: Listener for monitoring DTD events
(3) Entityresolver monitoring entity handling events
(4) ErrorHandler monitoring error
JAXP provides the DefaultHandler class for monitoring. The DefaultHandler class implements the ContentHandler, Dtdhandler, Entityresolver, and ErrorHandler interfaces and provides an empty implementation for the methods contained in these interfaces. As a result, developers only need to write a class that inherits from DefaultHandler and overrides the listening methods that they care about, without having to provide implementations for each method.

General steps for Sax parsing XML documents :

(1) Create SAXParserFactory object (SAX parser Factory) by SAXParserFactory's Newinstance () method
(2) Create a SAXParser object (SAX parser) from the SAXParserFactory object's Newsaxparser ()
(3) Parse the XML document with the parser () method of the SAXParser object, and the second parameter of the method is passed to a DefaultHandler object

In conclusion, we enclose the code for your reference:

Object types in a 1.DOM tree

When parsing an XML document using the DOM, the entire XML document is converted into a DOM tree, and the DOM parser translates the nodes of the XML document into each node of the DOM tree.

The DOM tree can not only describe the structure of XML document, but also have the characteristics of the object model, the process of transforming XML document into DOM tree is the process of object-based document model.

2.DOM parser (similar to SAX parser)

The following three steps allow you to create a DOM parser and complete the process of making an XML document model object.

(1) Create a DOM parser factory object by calling the Newinstance () method of the Documentbuilderfactory class.

(2) Create a DOM parser object by invoking the Newdocumentbuilder () method of the DOM parser factory object.

(3) by invoking the parse () method of the DOM parser object, the process of document model object is completed, and the XML document is parsed into the document object.

Attached code:

All in all, we must have a certain understanding of XML parsing, but want to really learn how to use XML and continue to learn with heart. Come on. (This article refers to the "still indifferent" relevant fragments, and on the basis of the collation plus personal understanding, hope to be useful to everyone)

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.