Android read/write XML (medium)

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


Note: This article from the http://www.moandroid.com.

 

Previously, in the android read/write XML (on) -- package description, we described in detail how to use the DOM method to read XML documents. Because the DOM method needs to load the entire XML document into the memory, this imposes a lot of restrictions on reading XML documents using dom for Android systems with relatively low memory usage. The sax method is used to read XML, which consumes less memory resources. Therefore, it is strongly recommended for embedded devices. Android is no exception, this article details how to use SAX to read XML documents in Android.

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 the sax API to process XML documents, we will introduce the event-driven processing mode used by sax.

Event-driven processing mode

The event-based processing mode focuses onEvent SourceAndEvent ProcessorTo work. An object that can generate events is calledEvent SourceObjects that can respond to events are calledEvent Processor. The event source and event processing object are connected 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 the event source calls a specific method in the event processor, it will pass an event sign and the status information of the response event, 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 uses the Parser () method to parse the XML document and generate events based on the document content. The event processor is Org. XML. the contenthander, dtdhander, errorhandler, and entityresolver interfaces in the sax package process different types of events generated by the event source during XML document parsing. The connection between the event source xmlreader and the four event processors is completed through 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 Org. XML. sax. the Helper package provides defaulthandler class, which inherits these four interfaces. In actual development, you can directly inherit from defaulthandler and implement relevant functions. Among the four interfaces, the most important is the contenthanlder interface. The following describes the methods:

The above is the process of parsing an XML document and event processing. Here we. XML. sax. in addition to the series of event processor registration methods set/getxxxx () described above, 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 better to be consistent with the XML document.
  2. Secondly, we started to implement event processing objects. Android SDK provided defaulthandler, which inherits all interfaces of contenthandler, dtdhandler, entityresolver, and errorhandler. Based on defaulthandler, we implement all contenthanlder interfaces as needed. here we need to pay attention to two problems:
    1. The sequence of event processing, where the processing time is located: The processing work completed in startdocument () initialization and enddocument (); startelement ()-characters ()-enddocument () it is a process of reading XML nodes. startelement () is used for initial judgment. characters () gets 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.

The above is the process of reading XML from Sax and displaying the final data. Note that the saxparserfactory class comes from 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 Dom. which method is suitable, you can deeply understand the differences in 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.