PULL解析器:
PULL解析器是Android内置的一种解析XML文件的解析器。PULL解析器的运行方式和SAX类似,都是基于事件的模式。不同的是,在PULL解析过程中,我们需要自己获取产生的事件然后做相应的操作,而不像SAX那样由处理器触发一种事件的方法,执行我们的代码。PULL解析器小巧轻便,解析速度快,简单易用。
PULL解析XML
pull方式,解析xml是通过 XmlPullParser 类实现
首先,通过XmlPullParser得到解析xml的实例 parser
接着,通过 parser设置输入 parser.setInput(is, "utf-8"),声明定义保存xml信息的数据结构(如:Book集合)
然后,通过 parser解析 START_DOCUMENT、START_TAG、TEXT、END_TAG、END_DOCUMENT等
代码:
public List<Book> parser(InputStream is) throws Exception {
List<Book> books = null;
Book book = null;
XmlPullParser parser = Xml.newPullParser();//由android.util.Xml创建一个XmlPullParser实例对象
parser.setInput(is, "UTF-8");//设置输入流并指明编码方式
int eventType = parser.getEventType();
while(eventType != XmlPullParser.END_DOCUMENT){
switch (eventType) {
case XmlPullParser.START_DOCUMENT:
books = new ArrayList<Book>();
break;
case XmlPullParser.START_TAG:
if(parser.getName().equals("book")){
book = new Book();
}else if(parser.getName().equals("id")){
eventType = parser.next();
book.setId(Integer.parseInt(parser.getText()));
}else if(parser.getName().equals("name")){
eventType = parser.next();
book.setName(parser.getText());
}else if(parser.getName().equals("price")){
eventType = parser.next();
book.setPrice(Float.parseFloat(parser.getText()));
}
break;
case XmlPullParser.END_TAG:
if(parser.getName().equals("book")){
books.add(book);
book = null;
}
break;
default:
break;
}
eventType = parser.next();
}
return books;
}
PULL创建XML:
pull方式,创建xml是通过 XmlSerializer 类实现
首先,通过XmlSerializer得到创建xml的实例 serializer
接着,通过 serializer设置输出 xmlSerializer.setOutput,xmlSerializer.startDocument("utf-8", null)设置xml属性等
然后,通过 serializer创建 startDocument、startTag、text、endTag、endDocument等
代码:
public String serialize(List<Book> books) throws Exception {
XmlSerializer serializer = Xml.newSerializer();////由android.util.Xml创建一个XmlSerializer实例对象
StringWriter writer = new StringWriter();
serializer.setOutput(writer);
serializer.setFeature("http://xmlpull.org/v1/doc/features.html#indent-output", true);//设置换行
serializer.startDocument("UTF-8", null);//<?xml version='1.0' encoding='UTF-8' standalone='yes' ?>
serializer.startTag("", "books");
for(Book book : books){
serializer.startTag("", "book");
serializer.attribute("", "id", book.getId()+"");
serializer.startTag("", "name");
serializer.text(book.getName());
serializer.endTag("", "name");
serializer.startTag("", "price");
serializer.text(book.getPrice() + "");
serializer.endTag("", "price");
serializer.endTag("", "book");
}
serializer.endTag("", "books");
serializer.endDocument();
return writer.toString();
}
PullBookParser.java
package com.sym.xml;
import java.io.InputStream;
import java.io.StringWriter;
import java.util.ArrayList;
import java.util.List;
import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlSerializer;
import android.util.Xml;
public class PullBookParser implements BookParser {
@Override
public List<Book> parser(InputStream is) throws Exception {
List<Book> books = null;
Book book = null;
XmlPullParser parser = Xml.newPullParser();//由android.util.Xml创建一个XmlPullParser实例对象
parser.setInput(is, "UTF-8");//设置输入流并指明编码方式
int eventType = parser.getEventType();
while(eventType != XmlPullParser.END_DOCUMENT){
switch (eventType) {
case XmlPullParser.START_DOCUMENT:
books = new ArrayList<Book>();
break;
case XmlPullParser.START_TAG:
if(parser.getName().equals("book")){
book = new Book();
}else if(parser.getName().equals("id")){
eventType = parser.next();
book.setId(Integer.parseInt(parser.getText()));
}else if(parser.getName().equals("name")){
eventType = parser.next();
book.setName(parser.getText());
}else if(parser.getName().equals("price")){
eventType = parser.next();
book.setPrice(Float.parseFloat(parser.getText()));
}
break;
case XmlPullParser.END_TAG:
if(parser.getName().equals("book")){
books.add(book);
book = null;
}
break;
default:
break;
}
eventType = parser.next();
}
return books;
}
@Override
public String serialize(List<Book> books) throws Exception {
XmlSerializer serializer = Xml.newSerializer();////由android.util.Xml创建一个XmlSerializer实例对象
StringWriter writer = new StringWriter();
serializer.setOutput(writer);
serializer.setFeature("http://xmlpull.org/v1/doc/features.html#indent-output", true);//设置换行
serializer.startDocument("UTF-8", null);//<?xml version='1.0' encoding='UTF-8' standalone='yes' ?>
serializer.startTag("", "books");
for(Book book : books){
serializer.startTag("", "book");
serializer.attribute("", "id", book.getId()+"");
serializer.startTag("", "name");
serializer.text(book.getName());
serializer.endTag("", "name");
serializer.startTag("", "price");
serializer.text(book.getPrice() + "");
serializer.endTag("", "price");
serializer.endTag("", "book");
}
serializer.endTag("", "books");
serializer.endDocument();
return writer.toString();
}
}
MainActivity.java修改的地方:(红色的地方)
public void read(View v){
try {
InputStream is = getAssets().open("books.xml");
// parser = new SaxBookParser();//创建SaxBookParser实例
// parser = new DomBookParser();
parser = new PullBookParser();
books = parser.parser(is);//解析输入流
StringBuffer sb = new StringBuffer();
sb.append("解析XML结果:" + "\n");
for(Book book : books){
sb.append(book.toString() + "\n");
Log.i(TAG, book.toString());
}
readxml.setText(sb.toString());
} catch (Exception e) {
Log.e(TAG, e.getMessage());
}
}
注意:这里的books.xml、Book.java和自定义解析器接口BookParser.java与 Android 创建与解析XML(一)---- SAX方式 中的是一样的
执行的结果图: