Android learning notes (16) ---- XML parsing in Android by pull

Source: Internet
Author: User
Tags tagname

/*************************************** **************************************** *************
* Author: conowen @ Dazhong
* E-mail: conowen@hotmail.com
* Http://blog.csdn.net/conowen
* Note: This article is original and only used for learning and communication. For more information, indicate the author and its source.

**************************************** **************************************** ************/

1. Simple Explanation of the XML document:

First, let's take a look at the section of an XML document:

<Wordbook> <item> <word> cupboard </word> <trans> <! [CDATA [n. Cupboard; cupboard]> </Trans> <phonetic> <! [CDATA [['k using B using D]> </phonetic> <tags> CET4-EASY </tags> </item> <word> A </Word> <trans> <! [CDATA [Art. 1. Any; each A: Zhu Form A: General Certificate | General Certificate of Origin | certificate of origin of the general license;: many | one after another | many]> </Trans> <phonetic> <! [CDATA [[ei]> </phonetic> <tags> CET4-EASY </tags> </item> <word> abandon </word> <trans> <! [CDATA [n. fanatic; let vt. abandon; abandon: Give Up | confirm the option is invalid | assign call abandon: The call is suspended. Abandon V: Give Up]> </Trans> <phonetic> <! [CDATA [[comment 'B got nd got N]> </phonetic> <tags> CET4-EASY </tags> </item> <word> ability </ word> <trans> <! [CDATA [n. capability, capability; Ability: capability | capability encapsulating ability: encapsulation inhibition limited ability: limited capacity]> </Trans> <phonetic> <! [CDATA [[comment 'biliti]> </phonetic> <tags> CET4-EASY </tags> </item>
</wordbook>

Model:

<Document tag> <sub-tag> <tag a> str1 <tag a/> <tag B> str2 <tag B/> <sub-tag> <tag> str1 <tag a/> <tag B> str2 <tag B/> <tag a/> <tag B> <tag a> str1 <tag a/> <tag B> str2 <tag B/> <sub-tag/> <document tag/>

In the preceding XML, the document label is <wordbook>. All other sub-tags in the document are included in the document <bookstore>. The document tag <wordbook> has four <item> sub-tags. Each <item> sub-tag has four nodes: <word>,
<Trans>, <phonetic>, and <tags> nodes. Each node contains the content of the node.

Note: If the STR content in the tag is a line break, after the XML file is parsed, the output is also a line break.

XML file: http://ishare.iask.sina.com.cn/f/20283647.html

2. XML parsing solution for the Android platform:

You can use the following XML parsing methods on the Android platform:

2.1. Simple API for XML (SAX)

The sax parsing method is event-driven. When you read the start or end of a document tag, there will be corresponding events. When you read the start or end tags of a subnode, there will also be corresponding events. When you read a node of a subnode, an event occurs. Because the event driver of the Android platform is based on callback functions, that is to say, an interface for callback functions is implemented when the above events are encountered.

2.2 Document Object Model (DOM)

When Dom parses XML, it regards the entire XML document as a node tree, directly reads the entire node tree into the memory, and then parses it. This advantage is that the operation is simple and convenient, however, the disadvantage is that for large XML documents, the chance of parsing is very slow and resource-consuming.

2.3. Pull parser attached to Android

The pull side is basically the same as the sax side.

In short, it is recommended that you use the sax or pull method for parsing large-volume XML files. For small-volume XML documents, you can choose Dom parsing.

3. Explanation of pull parsing Methods

Note: The labels mentioned below include all the labels in the sub-tag and sub-tag.

Pull parsing methods:

Xmlresourceparser xrp; // defines an XML resource parser.

Xmlresourceparser. start_document encounters the start of the document tag

Xmlresourceparser. end_document: End of the document tag

Xmlpullparser. start_tag: Start of a tag

Xmlpullparser. end_tag: End xrp of a tag. getname () gets the tag name xrp. nexttext (); get the content in the tag (STR) (not xrp. gettext ();) xrp. next (); Continue to read the next tag

Xrp. nexttag (); Continue to read the next tag, which is basically the same as next ()

Next we will write a parsing "English dictionary. xml" to implement an android dictionary program. For more information, see annotations.

:

/* Author: conowen * Date: 2012.4.1 */package COM. conowen. dictionary; import android. app. activity; import android. content. res. xmlresourceparser; import android. OS. bundle; import android. view. view; import android. view. view. onclicklistener; import android. widget. button; import android. widget. edittext; import android. widget. textview; public class dictionaryactivity extends activity {string input = NULL; string mat Ch = NULL; edittext ET = NULL; button search = NULL; button del = NULL; textview display = NULL;/** called when the activity is first created. * // @ overridepublic void oncreate (bundle savedinstancestate) {super. oncreate (savedinstancestate); setcontentview (R. layout. main); ET = (edittext) findviewbyid (R. id. et); Search = (button) findviewbyid (R. id. search); del = (button) findviewbyid (R. id. del); display = (textv Iew) findviewbyid (R. id. TV); search. setonclicklistener (New onclicklistener () {@ overridepublic void onclick (view v) {// todo auto-generated method stubinput = et. gettext (). tostring (); // create an XML folder under the res directory of the current project, and drag cet4.xml into xmlresourceparser xrp = getresources (). getxml (R. XML. CET4); // define an XML resource parser try {stringbuilder strbuilder = new stringbuilder (""); While (xrp. geteventtype ()! = Xmlresourceparser. end_document) {// If (xrp. geteventtype () = xmlresourceparser. start_tag) {// when a start tag is encountered, it includes all the tags in the sub-tag and the sub-tag string tagname = xrp. getname (); // obtain the tag name if (tagname. equals ("word") {// retrieves the word match = xrp. nexttext (); // read the content in the tag, that is, the word if (match. equals (input) {// If the obtained word is the same as the input word xrp. next (); // read the next tag, that is, the <trans> translation strbuilder. append (xrp. nexttext (); // extract the translation display. settext (strbuilder); // display translation break ;}} xrp. next (); // read the next tag }}catch (exception e) {// todo: handle exception }}); Del. setonclicklistener (New onclicklistener () {@ overridepublic void onclick (view v) {// todo auto-generated method stubdisplay. settext (""); et. settext ("");}});}}

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.