Pull解析器的运行方式与 SAX 解析器相似,提供了类似事件,如:开始元素和结束元素事件,使用parser.next()可以进入下一个元素并触发相应事件。
跟SAX不同的是, Pull解析器产生的事件是一个数字,而非方法,因此可以使用一个switch对感兴趣的事件进行处理。当元素开始解析时,调用parser.nextText()方法可以获取下一个Text类型节点的值
解析的时候会把空格也算在内,最后把源文件整一下
点击button开始解析
public class MainActivity extends Activity {
public static String TAG = "pullparser";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void parsebyPull(View v) throws IOException{
//得到解析器XMLPullParser
XmlPullParser pullparser = Xml.newPullParser();
try {
//设置要读取的文件
//以字节流的形式输入内容。编码要与xml中声明的编码保持一致
InputStream in = new FileInputStream("data/data/com.example.pullparser/files/information.xml");
pullparser.setInput(in, "UTF-8");
//得到读取到的某部分的类型
int evenType = pullparser.getEventType();
while(evenType!=XmlPullParser.END_DOCUMENT){
if (evenType==XmlPullParser.START_DOCUMENT) {
Log.i(TAG, "START_DOCUMENT") ;
}
if (evenType==XmlPullParser.START_TAG) {
Log.i(TAG, "START_TAG");
}
if (evenType==XmlPullParser.TEXT) {
Log.i(TAG, "TEXT") ;
}
if (evenType==XmlPullParser.END_TAG) {
Log.i(TAG, "END_TAG");
}
evenType=pullparser.next();
}
if (evenType==XmlPullParser.END_DOCUMENT) {
Log.i(TAG, "END_DOCUMENT") ;
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (XmlPullParserException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
我们也可以把解析封装到一个bean里
package com.example.bean;
public class Book {
private String bookname;
private String author;
private String priceString;
@Override
public String toString() {
return "Book [bookname=" + bookname + ", author=" + author
+ ", priceString=" + priceString + "]";
}
public String getBookname() {
return bookname;
}
public void setBookname(String bookname) {
this.bookname = bookname;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
public String getPriceString() {
return priceString;
}
public void setPriceString(String priceString) {
this.priceString = priceString;
}
}
要被解析的xml,当然这个文件应该是导入到data下的app文件夹里
public class MainActivity extends Activity {
public static String TAG = "pullparser";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void parsebyPull(View v) throws IOException{
//得到解析器XMLPullParser
XmlPullParser pullparser = Xml.newPullParser();
List<Book> booklist= new ArrayList<Book>();
Book book = null;
try {
//设置要读取的文件
//以字节流的形式输入内容。编码要与xml中声明的编码保持一致
InputStream in = new FileInputStream("data/data/com.example.pullparser/files/books.xml");
pullparser.setInput(in, "UTF-8");
//得到读取到的某部分的类型
int evenType = pullparser.getEventType();
while(evenType!=XmlPullParser.END_DOCUMENT){
if (evenType==XmlPullParser.START_DOCUMENT) {
Log.i(TAG, "START_DOCUMENT") ;
}
if (evenType==XmlPullParser.START_TAG) {
Log.i(TAG, "START_TAG"+pullparser.getName()) ;
if ("book".equals(pullparser.getName())) {
book = new Book();
}
if ("bookname".equals(pullparser.getName())) {
book.setBookname(pullparser.nextText());
}
if ("author".equals(pullparser.getName())) {
book.setAuthor(pullparser.nextText());
}
if ("price".equals(pullparser.getName())) {
book.setPriceString(pullparser.nextText());
}
}
if (evenType==XmlPullParser.TEXT) {
Log.i(TAG, "TEXT" +pullparser.getText()) ;
}
if (evenType==XmlPullParser.END_TAG) {
Log.i(TAG, "END_TAG"+pullparser.getName()) ;
if ("book".equals(pullparser.getName())) {
booklist.add(book);
book = null;
}
}
evenType=pullparser.next();
}
if (evenType==XmlPullParser.END_DOCUMENT) {
Log.i(TAG, "END_DOCUMENT") ;
}
for ( Book book2 : booklist) {
Log.i(TAG, book2.toString() ) ;
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (XmlPullParserException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}