Android read/write XML (medium) -- sax

Source: Internet
Author: User
Document directory
  • Event-driven processing mode
  • Follow the process of application development to briefly introduce how to use sax.
  • Summary

Read and Write XML (on) in Android )--
Package Description
.
The DOM method reads XML documents. Because the DOM method needs to load the entire XML file into the memory, it occupies a lot of system resources, which is used by Android systems with relatively insufficient memory.
Reading XML documents using the DOM method has a lot of restrictions.
Using the sax method to read XML, the memory usage is relatively small, so it is strongly recommended to use in embedded devices, Android is no exception, this article details how to make
Read XML documents with Sax.

Sax adopts an event-driven processing method. It converts an XML document into a series of events, which are determined by a separate event processor. To learn how to use SAX
API processing XML document. Here we will introduce the event-driven processing mode used by sax.

Event-driven processing mode

The event-based processing mode focuses onEvent Source
AndEvent Processor
To work. One can generate
The event object is calledEvent Source
Objects that can respond to events are calledEvent Processor
. Event Source
It is connected to the event processing object through the event registration method in the event source. When an event source is generated, the corresponding method of the event processor is called to process an event. When an event source calls an event processor
When determining the method, an event flag and the status information of the response event will be passed, so that the event processor can determine its own behavior based on the event information.

In the sax interface, the event source is xmlreader In the org. xml. Sax package. It parses the XML document using the Parser () method and produces
Generate an event. The event processor is contenthander, dtdhander, errorhandler, and
Entityresolver interfaces process different types of events generated by the event source during XML document parsing. The event source xmlreader handles the four events.
The connection is completed by the corresponding event processor registration method setxxxx () in xmlreader. For details, see the following table:


The above four event source processor interfaces do not need to be directly inherited from these four interfaces during development, because the org. xml. Sax. helper package provides a class for us.
Defaulthandler inherits these four interfaces. In actual development, it can directly inherit from defaulthandler and implement relevant functions. Of the four interfaces
The contenthanlder interface is important. The methods are described as follows:

The above is the process of parsing an XML document and event processing. Here we will add a description of the methods in org. xml. Sax. xmlreader: in addition to the series
In addition to the event processor registration method set/getxxxx (), there is also an important function: parse (inputsource input)-parse an XML
Document to parse an XML document.

Follow the process of application development to briefly introduce how to use sax.
  1. First, write the structure of the XML document, write the specific data structure according to the structure of the XML document, similar to: xxxxlist, xxxxitem, it is best to keep one with the XML document
    To.
  2. Secondly, we started to implement event processing objects. Android SDK provided defaulthandler, which inherits contenthandler.
    , Dtdhandler, entityresolver,
    All the interfaces of errorhandler. Based on defaulthandler, we implement all contenthanlder interfaces as needed.
    Two problems:

    1. Event processing order, processing time location: generally in startdocument () initialization, finishing in enddocument ()
      Startelement ()-characters ()-enddocument () is a process for reading XML nodes. startelement () Uses
      For initial judgment, characters () obtains the character data of the node, and enddocument () writes the data into the data structure.
    2. Exception saxexception handling;
  3. Then, the XML object parsing process is as follows:
    Saxparserfactory factory = saxparserfactory. newinstance ();
    Saxparser parser = factory. newsaxparser ();
    Xmlreader = parser. getxmlreader ();
    URL rrl = new URL (urlstring );
    Inputsource is = new inputsource (URL. openstream ());
    Xmlreader. setcontenthanlder (XXXX );
    Xmlreader. parse (is );
  4. Finally, display data in the activity. Generally, the data read in XML is first selected, and the appropriate adaper is selected to encapsulate the data. Then, the listview control is used to display the data.
    Data.

The above is the process of reading XML from Sax and displaying the final data. Note that the saxparserfactory class comes from
In the javax. xml. parsers package.

Summary

The method of reading XML documents by using sax occupies less memory, but the development process is much more complicated than the Dom. which method is suitable for reading this document can be deeply understood during the development process?
.

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.