1 package com.example.bean; 2 3 public class Book { 4 5 private String bookname; 6 private String author; 7 private String priceString; 8 9 10 @Override 11 public String toString() { 12 return "Book [bookname=" + bookname + ", author=" + author 13 + ", priceString=" + priceString + "]"; 14 } 15 16 public String getBookname() { 17 return bookname; 18 } 19 public void setBookname(String bookname) { 20 this.bookname = bookname; 21 } 22 public String getAuthor() { 23 return author; 24 } 25 public void setAuthor(String author) { 26 this.author = author; 27 } 28 public String getPriceString() { 29 return priceString; 30 } 31 public void setPriceString(String priceString) { 32 this.priceString = priceString; 33 } 34 35 36 }
1 package com.example.pullparser; 2 3 import java.io.FileInputStream; 4 5 import java.io.IOException; 6 import java.io.InputStream; 7 import java.util.ArrayList; 8 import java.util.List; 9 10 import org.xmlpull.v1.XmlPullParser; 11 import org.xmlpull.v1.XmlPullParserException; 12 13 import com.example.bean.Book; 14 import com.example.day04_05pullparser.R; 15 16 import android.app.Activity; 17 import android.os.Bundle; 18 import android.util.Log; 19 import android.util.Xml; 20 import android.view.Menu; 21 import android.view.MenuItem; 22 import android.view.View; 23 24 public class MainActivity extends Activity { 25 public static String TAG ="pullParser"; 26 27 28 @Override 29 protected void onCreate(Bundle savedInstanceState) { 30 super.onCreate(savedInstanceState); 31 setContentView(R.layout.activity_main); 32 } 33 34 35 public void parsebyPull(View v){ 36 37 XmlPullParser pullParser = Xml.newPullParser(); 38 ListbookList = new ArrayList (); 39 Book book = null; 40 41 try { 42 InputStream in = new FileInputStream("data/data/com.example.day04_05pullparser/files/books.xml"); 43 pullParser.setInput(in, "UTF-8"); 44 45 int evenType = pullParser.getEventType(); 46 47 while(evenType != XmlPullParser.END_DOCUMENT){ 48 49 if(evenType == XmlPullParser.START_DOCUMENT){ 50 Log.i(TAG, "START_DOCUMENT"); 51 } 52 if(evenType == XmlPullParser.START_TAG){ 53 Log.i(TAG,"START_TAG" + pullParser.getName()); 54 if("book".equals(pullParser.getName())){ 55 book.setBookname(pullParser.nextText()); 56 Log.i(TAG, book.getBookname()); 57 } 58 if("bookname".equals(pullParser.getName())){ 59 book.setAuthor(pullParser.nextText()); 60 Log.i(TAG, book.getAuthor()); 61 } 62 if("author".equals(pullParser.getName())){ 63 book.setAuthor(pullParser.nextText()); 64 Log.i(TAG, book.getAuthor()); 65 } 66 if("price".equals(pullParser.getName())){ 67 book.setPriceString(pullParser.nextText()); 68 Log.i(TAG, book.getPriceString()); 69 } 70 71 } 72 if(evenType == XmlPullParser.TEXT){ 73 Log.i(TAG, "TEXT" + pullParser.getText()); 74 } 75 if(evenType == XmlPullParser.END_TAG){ 76 Log.i(TAG, "END_TAG" + pullParser.getName()); 77 78 if("book".equals(pullParser.getName())){ 79 bookList.add(book); 80 book = null; 81 } 82 } 83 evenType = pullParser.next(); 84 Log.i(TAG, "eventType:" + evenType); 85 86 } 87 88 if(evenType == XmlPullParser.END_DOCUMENT){ 89 Log.i(TAG, "END_DOCUMENT"); 90 } 91 for(Book book2:bookList){ 92 Log.i(TAG, book2.toString()); 93 } 94 } catch (XmlPullParserException e) { 95 // TODO Auto-generated catch block 96 e.printStackTrace(); 97 } catch (IOException e) { 98 // TODO Auto-generated catch block 99 e.printStackTrace(); 100 } 101 } 102 }