Android-three steps for parsing XML data

Source: Internet
Author: User

The specific steps for DOM parsing are as follows: 1. first create a DocumentBuilderFactory instance using DocumentBuilderFactory 2, then create DocumentBuilder3 using DocumentBuilderFactory, and then load the XML Document (Document ), 4. Obtain the document's root node (Element). 5. Then, obtain the list of all the child nodes (NodeList) at the root node. 6. Then, obtain the node to be read from the child node list. The specific processing steps when using the SAX parsing are as follows: 1. Create the SAXParserFactory object 2. According to the SAXParserFactory. newSAXParser () method return a SAXParser parser 3 get the event source object XMLReader4 according to the SAXParser parser instantiate a DefaultHandler object 5 connect the event source object XMLReader to the event processing class DefaultHandler 6 call the parse method of XMLReader from the input the xml data obtained from the source 7 returns the data set we need through DefaultHandler. The basic processing method of PULL Parsing is as follows: 1. When you navigate to XmlPullParser. START_DOCUMENT, you can skip this step. Of course, you can instantiate a collection object. 2: When you navigate to XmlPullParser. START_TAG, you can determine whether it is a river tag. If yes, the river object is instantiated and the getAttributeValue method is called to obtain the attribute values in the tag. 3: When you navigate to other labels, such as Introduction, determine whether the river object is empty. If not, extract the content in Introduction. The nextText method is used to obtain the content of the text node. 4: it will certainly navigate to XmlPullParser. END_TAG. The end of an END_TAG must begin with an END_TAG. Here we need to determine whether the river end label is used. If yes, the river object is saved in the list set and the river object is set to null. comparison and summary of several parsing technologies: for Android mobile devices, because the device resources are precious and the memory is limited, we need to select a suitable technology to parse XML, this will help increase the access speed. 1. When processing XML files, DOM parses the XML files into a tree structure and puts them in the memory for processing. When the XML file is small, we can select DOM because it is simple and intuitive. Www.2cto. com2 SAX uses an event as the parsing XML file mode. It converts an XML file into a series of events, which are determined by different event processors. When the XML file is large, it is reasonable to select the SAX technology. Although the amount of code is large, it does not need to load all XML files into the memory. This is more effective for limited Android memory, and Android provides a traditional method of using SAX and a convenient SAX package. 3. XML pull parsing does not end listening elements like SAX parsing, but completes most of the processing at the beginning. This facilitates reading XML files early and greatly reduces the parsing time. This optimization is especially important for mobile devices with relatively high connection speeds. The XML Pull parser is more effective when the XML document is large but only a part of the document is required.

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.