Android parsing XML with sax

Source: Internet
Author: User
Keywords Nbsp; xml name Tag this
Tags android content document file it is memory mobile name

Sax is an XML parser that is fast and consumes less memory, and is ideal for mobile devices such as Android. The Sax parsing XML file takes event-driven, that is, it does not need to parse through the entire document, and in the process of parsing the document in content order, sax determines whether the currently-read character is legitimate in the XML syntax, and triggers the event if it is compliant. The so-called events, in fact, are some callback (callback) methods, these methods (events) are defined in the ContentHandler interface. Here are some common methods for ContentHandler interfaces:

Startdocument ()

When you encounter the beginning of the document, call this method, you can do some preprocessing work.

Enddocument ()

Corresponding to the above method, when the end of the document, call this approach, you can do some cleanup work.

Startelement (String NamespaceURI, String LocalName, String qName, Attributes atts)

This method is triggered when a start tag is read. NamespaceURI is a namespace, LocalName is a label name without a namespace prefix, QName is a label name with a namespace prefix. All property names and corresponding values can be obtained by Atts. Note that one important feature of Sax is its streaming, when a tag is encountered, it does not record the previously encountered tags, that is, in the Startelement () method, all you know is the name and attributes of the tag, as for the nested structure of the tag, The name of the top tag, whether there are any other structure-related information, etc., are unknown and require your program to complete. This makes it easy for sax to be programmed without DOM.

EndElement (String uri, String localname, string name)

This method corresponds to the above square method, which is called when the end tag is encountered.

Characters (char] ch, int start, int length)

This method is used to process what is read in an XML file, the first parameter is the string content of the file, and the next two parameters are the starting position and length of the read string in the array, and the content can be obtained using the new string (ch,start,length).

Person.xml

View plain <?xml version= "1.0" encoding= "UTF-8"?> <persons> <person id= "> <name> Zhang < /name> <age>60</age> </person> <person id= "2" > <
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.