Android self-learning notes-9-Pull processing XML

Source: Internet
Author: User

There are three ways to process XML in android: DOM, SAX, and PUll. Here I will briefly introduce the Pull method. The other two methods can be used to search for information by yourself.

The Pull method is similar to the SAX principle in my understanding. It does not load the entire XML once. Instead, it loads a part of the content from the beginning, and then judges the node through some events, for example, there will be a document start/end event, tag start/end event, etc. By judging the event type and content, you can determine the XML node that is currently arriving, and then construct the data. The following is a simple example. In this example, only the START_TAG event is used:

Package com. mxy. pull; import java. io. IOException; import java. io. inputStream; import java. util. arrayList; import java. util. list; import org. xmlpull. v1.XmlPullParser; import org. xmlpull. v1.XmlPullParserException; import org. xmlpull. v1.XmlPullParserFactory; import com. mxy. mainActivity; import android. util. log; import android. util. xml; public class PullParseXml {public void PullParseXML () {XmlPullParser parser = Xml. newPullParser (); InputStream ins = MainActivity. class. getClassLoader (). getResourceAsStream ("data. xml "); try {parser. setInput (ins, "UTF-8"); int type = parser. getEventType (); // when the document ends, exit the loop while (type! = XmlPullParser. END_DOCUMENT) {String name = null; String id = null; switch (type) {case XmlPullParser. START_TAG: if ("student ". equals (parser. getName () {id = parser. getAttributeValue (0); Log. I ("mxy", "student id =" + id);} else if ("name ". equals (parser. getName () {name = parser. nextText (); Log. I ("mxy", "name =" + name);} break; case XmlPullParser. END_TAG: // process the TAG end event break; case XmlPullParser. TEXT: break; // another event default: break;} // This sentence is very important. Set the current node type to the next one to enter the next cycle type = parser. next () ;}} catch (Exception e) {// TODO Auto-generated catch blocke. printStackTrace ();}}}
The following is a brief introduction of constructing an XML file. The simplest thing is that we can use strings to splice a piece of XML. This method is the simplest and easiest to understand, but it is quite easy to make mistakes. So we can use XmlSerializer to construct an XML data.

The following is a small example to generate a simple XML file and save it to an external SD card:

/*** Xml serial number * 1 directly spell the string * 2 Use XmlSerializer for serial number */private void xmlSerializerMethod () {XmlSerializer serializer = Xml. newSerializer (); File file = new File (Environment. getExternalStorageDirectory (), "backup. xml "); // The address obtained here is/storage/sdcard/Log. I ("mxy", Environment. getExternalStorageDirectory (). toString (); try {// you need to set an Output for serializer. Otherwise, the serialized xml file cannot be retrieved. StringWriter writer = new StringWriter (); serializer. setOutput (writer); FileOutputStream fos = new FileOutputStream (file); serializer. startDocument ("UTF-8", true); serializer. setOutput (fos, "UTF-8"); serializer. startTag (null, "sms"); serializer. startTag (null, "name"); serializer. attribute (null, "id", "1"); serializer. text ("hello"); serializer. endTag (null, "name"); serializer. endTag (null, "sms"); serializer. endDocument (); // write data to the file fos. write (writer. toString (). getBytes (); fos. close ();} catch (Exception e) {// TODO Auto-generated catch blocke. printStackTrace ();}}
I may not be very clear about it. For more information, see http://blog.csdn.net/liuhe688/article/details/6415593.

Example project: http://download.csdn.net/detail/mengxiangyue/7020131

Reprinted please indicate the source: http://blog.csdn.net/mengxiangyu

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.