Working principle of PULL: XML pull provides the Start Element and end element. When an element starts, you can call parser. nextText to extract all characters from the XML document. An EndDocument event is automatically generated when a file is parsed to the end. Common XML pull interfaces and classes: XmlPullParser: This parser is an interface for parsing functions defined in org. xmlpull. v1. XmlSerializer: it is an interface that defines the sequence of XML Information sets. XmlPullParserFactory: this class is used to create an XML Pull parser in XMPULL V1 API. XmlPullParserException: throws errors related to a single XML pull parser. The Running Method of the PULL parser is similar to that of the SAX Parser, and is based on the event mode. The difference is that in the PULL parsing process, numbers are returned, and we need to obtain the generated events and perform corresponding operations, instead of using a processor to trigger an event like SAX, run our code: START_DOCUMENT is returned when the xml declaration is read; END_DOCUMENT is returned when the end is finished; START_TAG is returned when the start label is finished; END_TAG is returned when the end label is finished; TEXT is returned when the end label is finished. Send a picture first: Book. xml [html] <? Xml version = "1.0" encoding = "UTF-8"?> <Books> <book> <id> 1 </id> <name> Android development example (first edition) </name> <price> 69.00 </price> </book> <id> 2 </id> <name> Android development example (second edition) </name> <price> 79.00 </price> </book> <id> 3 </id> <name> Android development example (Third edition) </name> <price> 89.00 </price> </book> </books> Book. java [java] public class Book {private int id; private String name; private float price; public int getId () {return id;} public void setId (Int id) {this. id = id;} public String getName () {return name;} public void setName (String name) {this. name = name;} public float getPrice () {return price;} public void setPrice (float price) {this. price = price;} [java]} PullXmlActivity. java [java] public class PullXmlActivity extends Activity {private List <Book> list = null; private PullXmlAdapter adapter; private ListView lv; @ Override p Rotected void onCreate (Bundle savedInstanceState) {// TODO Auto-generated method stub super. onCreate (savedInstanceState); setContentView (R. layout. listview); lv = (ListView) findViewById (R. id. listView); InputStream in; try {in = this. getAssets (). open ("books. xml "); list = pullxml (in);} catch (IOException e) {// TODO Auto-generated catch block e. printStackTrace ();} catch (Exception e) {// TODO Auto- Generated catch block e. printStackTrace ();} adapter = new PullXmlAdapter (PullXmlActivity. this, list); lv. setAdapter (adapter);}/** get xml file * 1. put the xml file in the xml folder of res (recommended). Use XmlResourceParser xmlParser = this. getResources (). getXml (R. xml. XXX); * 2. put the xml file in the raw xml folder and use InputStream inputStream = this. getResources (). openRawResource (R. raw. XXX); * 3. put the xml file in the assets folder (I found that the XML document obtained through this method cannot carry the first line: <? Xml version = "1.0" encoding = "UTF-8"?>, * Otherwise, an error is reported. The specific cause is unknown: InputStream inputStream = getResources (). getAssets (). open (fileName); * 4. the xml file is placed on the SD card, and the path is modified according to the actual project. The root directory of the SDcard is * String path = Environment. getExternalStorageDirectory (). toString (); * File xmlFlie = new File (path + fileName); * InputStream inputStream = new FileInputStream (xmlFlie); */public List <Book> pullxml (InputStream in) throws Exception {List <Book> list = null; Book book = nu Ll; // by android. util. xml creates an XmlPullParser instance XmlPullParser parser = Xml. newPullParser (); // sets the input stream and specifies the encoding method parser. setInput (in, "UTF-8"); // generate the first event int eventType = parser. getEventType (); while (eventType! = XmlPullParser. END_DOCUMENT) {switch (eventType) {// determine whether the current event is a document start event case XmlPullParser. START_DOCUMENT: list = new ArrayList <Book> (); // initializes the break of the list set; // determines whether the current event is a tag element. Start the event case XmlPullParser. START_TAG: if (parser. getName (). equals ("book") {// determines whether the start tag element is book = new Book ();} else if (parser. getName (). equals ("id") {eventType = parser. next (); // obtain the attribute value of the book tag and set the id book of the book. setId (Integer. parseInt (parser. getText ();} else if (parser. getName (). equals ("name") {// determines whether the start tag element is book eventType = parser. next (); book. setName (parser. getText ();} else if (parser. getName (). equals ("price") {// determine whether the start tag element is price eventType = parser. next (); book. setPrice (Float. parseFloat (parser. getText ();} break; // determines whether the current event is a Tag Element end event case XmlPullParser. END_TAG: if (parser. getName (). equals ("book") {// determines whether the end tag element is a book list. add (book); // add the book to the books collection book = null;} break;} // enter the next element and trigger the event eventType = parser. next ();} return list;} PullXmlAdapter. java [java] public class PullXmlAdapter extends BaseAdapter {private List <Book> list = null; private Context context = null; private LayoutInflater inflater = null; public PullXmlAdapter (Context context, list <Book> list) {// TODO Auto-generated constructor stub this. context = context; this. list = list ;}@ Override public int getCount () {// TODO Auto-generated method stub return list. size () ;}@ Override public Object getItem (int arg0) {// TODO Auto-generated method stub return arg0 ;}@ Override public long getItemId (int arg0) {// TODO Auto-generated method stub return arg0;} @ Override public View getView (int arg0, View arg1, ViewGroup arg2) {// TODO Auto-generated method stub inflater = LayoutInflater. from (context); View v = inflater. inflate (R. layout. listview_item, null); TextView TV = (TextView) v. findViewById (R. id. lisview_item_ TV); TV. setText (list. get (arg0 ). getName (); return v ;}}