Android: Pull Parse XML
In the previous article, I wrote the SAX parsing XML. It seems that the Pull method is very similar to the SAX method. It only requires an auxiliary class. After parsing an event, I write code in the callback method, pull returns an int value by calling the event type method, and then writes code based on the value.
First, define an XML file: Student. xml. Note that the file is created instead of xml.
Zhang San
Male
18
Li Si
Female
19
Wang Wu
Male
20
Take parsing the first data as an example:
Zhang San
Male
18
The entire process is: int event = parser. getEventType () generates an XmlPullParser. START_DOCUMENT: instantiate List → event = parser. next () triggers the next event to generate XmlPullParser. START_TAG, the element name is student. instantiate student and assign the element attribute to the corresponding field → event = parser. next () triggers the next event to generate XmlPullParser. START_TAG, the element name is name, and the text information is assigned to the corresponding field → event = parser. next () triggers the next event to generate XmlPullParser. END_TAG, no response → ...... → finallyXmlPullParser. END_TAG, the element name is student. Store the student object in the list and set it to null.Finally, a Button is used in the activity to parse the data, which uses several classes.
Package com. example. xml_sax_demo_1; import java. io. IOException; import java. io. inputStream; import java. util. arrayList; import java. util. list; import javax. xml. parsers. parserConfigurationException; import javax. xml. parsers. SAXParser; import javax. xml. parsers. SAXParserFactory; import org. xml. sax. SAXException; import org. xmlpull. v1.XmlPullParser; import org. xmlpull. v1.XmlPullParserFactory; import android. support. v7.app. actionBarActivity; import android. OS. bundle; import android. util. xml; import android. view. menu; import android. view. menuItem; import android. view. view; import android. view. view. onClickListener; import android. widget. button; public class MainActivity extends ActionBarActivity {@ Overrideprotected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); Button button = (Button) findViewById (R. id. button); button. setOnClickListener (new OnClickListener () {@ Overridepublic void onClick (View arg0) {// TODO Auto-generated method stubtry {readXML ();} catch (Exception e) {// TODO Auto-generated catch blocke. printStackTrace () ;}});} private void readXML () throws Exception {Student student = null; List
List = null; XmlPullParserFactory xmlPullParserFactory = XmlPullParserFactory. newInstance (); XmlPullParser parser = xmlPullParserFactory. newPullParser (); // instantiate XmlPullParserInputStream stream = this. getClass (). getClassLoader (). getResourceAsStream ("Student. xml "); // get the input stream parser. setInput (stream, "UTF-8"); // two parameter input streams, encoding format int event = parser. getEventType (); // generates the first event while (event! = XmlPullParser. END_DOCUMENT) {switch (event) {case XmlPullParser. START_DOCUMENT: list = new ArrayList
(); // The document starts to instantiate listbreak; case XmlPullParser. START_TAG: if ("student ". equals (parser. getName () {student = new Student (); student. setId (Integer. parseInt (parser. getAttributeValue (0);} if (student! = Null) {if ("name ". equals (parser. getName () {student. setName (parser. nextText ();} else if ("sex ". equals (parser. getName () {student. setSex (parser. nextText ();} else if ("age ". equals (parser. getName () {student. setAge (Integer. parseInt (parser. nextText () ;}} break; case XmlPullParser. END_TAG: if ("student ". equals (parser. getName () {list. add (student); student = null;} break;} event = parser. next (); // enter the next element and trigger the corresponding event} for (Student stu: list) {System. out. println (stu. toString ());}}}
Result:
Summary: There are several important constants in the XmlPullParser class: START_DOCUMENT, END_DOCUMENT, START_TAG, and END_TAG. In fact, there is also a Text. Several important methods: getName (), get the element name; nextText (), get the next text information; next (), read the next resolution status.