Android parses xml using pull

Source: Internet
Author: User

1. Theoretically, the Pull parser runs in a similar way as the SAX Parser. It provides similar events, such as the start element and end element events. You can use parser. next () to enter the next element and trigger the corresponding event. Unlike SAX, the events generated by the Pull parser are numbers rather than methods. Therefore, you can use a switch to process events of interest. When parsing an element, call parser. nextText () to obtain the value of the next Text node. The project structure is as follows: why does image put xml under the assets Directory? The difference between it and the res/values directory is that the system does not allocate IDs. It seems that the problem has not been solved and no information has been found. Ii. code package com. example. and_0003; import java. io. inputStream; import java. util. list; import com. hpu. entity. student; import com. hpu. util. pullService; import android. app. activity; import android. content. res. assetManager; import android. OS. bundle; import android. util. log; public class MainActivity extends Activity {/** Called when the activity is first created. * // @ Override public void onCreate (Bundle save DInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); AssetManager asset = getAssets (); try {InputStream input = asset. open ("student. xml "); List <Student> list = PullService. getStudents (input); for (Student stu: list) {Log. e ("StudentInfo", "Person ID:" + stu. getId () + "," + stu. getName () + "," + stu. getAge () + "," + stu. getSex () ;}} catch (Throwable e) {E. printStackTrace () ;}}1: <? Xml version = "1.0" encoding = "UTF-8"?> 2: <students> 3: <student id = "20110806100"> 4: <name> James </name> 5: <age> 22 </age> 6: <sex> male </sex> 7: </student> 8: <student id = "20110806101"> 9: <name> Xiao LI </name> 10: <age> 24 </age> 11: <sex> male </sex> 12: </student> 13: <student id = "20110806102"> 14: <name> Lili </name> 15: <age> 21 </age> 16: <sex> female </sex> 17: </student> 18: </students> + View Code 1: package com. hpu. util; 2: 3: import java. io. inputStream; 4: I Mport java. util. arrayList; 5: import java. util. list; 6: 7: import org. xmlpull. v1.XmlPullParser; 8: import org. xmlpull. v1.XmlPullParserFactory; 9: 10: import com. hpu. entity. student; 11: 12: import android. util. xml; 13: 14: public class PullService {15: 16: // use XmlPullParser to parse the XML file 17: public static List <Student> getStudents (InputStream inStream) 18: throws Throwable {19: List <Student> students = Null; 20: Student mStudent = null; 21: 22: // ========== create XmlPullParser in either of the following ways: // Method 1: Use the factory class XmlPullParserFactory 24: XmlPullParserFactory pullFactory = XmlPullParserFactory. newInstance (); 25: XmlPullParser parser = pullFactory. newPullParser (); 26: // Method 2: Use the Android Utility Class. util. xml 27: // XmlPullParser parser = Xml. newPullParser (); 28: 29: // parse file input stream 30: parser. setInput (inStream, "U TF-8 "); 31: // generates the first event 32: int eventType = parser. getEventType (); 33: // The 34: while (eventType! = XmlPullParser. END_DOCUMENT) {35: switch (eventType) {36: // trigger the start document event 37: case XmlPullParser. START_DOCUMENT: 38: students = new ArrayList <Student> (); 39: break; 40: // trigger the Start Element event 41: case XmlPullParser. START_TAG: 42: // get the name of the element currently pointed to by the parser 43: String name = parser. getName (); 44: if ("student ". equals (name) {45: // get the element value of id through the parser, and set student id 46: mStudent = new Student (); 47: mStudent. setId (parser. get AttributeValue (0); 48:} 49: if (mStudent! = Null) {50: if ("name ". equals (name) {51: // gets the value of the next text node that the parser currently points to. 52: mStudent. setName (parser. nextText (); 53:} 54: if ("age ". equals (name) {55: // gets the value of the next text node that the parser currently points to the element 56: mStudent. setAge (new Short (parser. nextText (); 57:} 58: if ("sex ". equals (name) {59: // gets the value of the next text node that the parser currently points to the element 60: mStudent. setSex (parser. nextText (); 61:} 62:} 63: break; 64: // trigger the end element event 65: case XmlPullParser. END_TAG: 66: // 67: if ("student ". equals (parser. getName () {68: students. add (mStudent); 69: mStudent = null; 70:} 71: break; 72: default: 73: break; 74:} 75: eventType = parser. next (); 76:} 77: return students; 78:} 79: 80:} 81:

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.