在Android中常用的XML解析器有三种:
- DOM解析器
- SAX解析器
- PULL解析器
下面记录一下PULL解析器的方式,PULL解析是在XML文档中寻找想要的标记,把需要的内容放入内存,而不是把整个文档都放入内存,这种方式比较适合手机等内存有限的小型的移动设备。
PULL解析器小巧轻便,解析速度快,简单易用,非常适合在Android移动设备中使用,Android系统内部在解析各种XML时也是用PULL解析器,Android官方推荐开发者们使用Pull解析技术。
Bean对象():
class SmsBean{
private String Address;
private String date;
private String body;
public String getBody() {
return body;
}
public void setBody(String body) {
this.body = body;
}
public String getDate() {
return date;
}
public void setDate(String date) {
this.date = date;
}
public String getAddress() {
return Address;
}
public void setAddress(String address) {
Address = address;
}
}
}
1.使用XmlSerializer写入Xml
格式的数据到文件中:
private void writeXmlToLocal(ArrayList<SmsBean> mSmsList){
try {
//1.获得序列化对象
XmlSerializer xmlSerializer = Xml.newSerializer();
File path = new File(Environment.getExternalStorageDirectory(),"sms.xml"); //构建一个文件路径
OutputStream out = new FileOutputStream(path); //获取文件输出流
xmlSerializer.setOutput(out,"utf-8"); //指定序列化对象输出的位置和编码
xmlSerializer.startDocument("utf-8",true); // 相当于 <?xml version="1.0" encoding="UTF-8" standalone="true"?>
xmlSerializer.startTag(null,"AllSms"); //null表示不包含namespace
int count = 0;
for(SmsBean sms:mSmsList){
xmlSerializer.startTag(null,"sms");
xmlSerializer.attribute(null,"num",String.valueOf(count++)); //给sms标签设置属性
xmlSerializer.startTag(null,"date");
xmlSerializer.text(sms.getDate());
xmlSerializer.endTag(null,"date");
xmlSerializer.startTag(null,"address");
xmlSerializer.text(sms.getAddress());
xmlSerializer.endTag(null,"address");
xmlSerializer.startTag(null,"body");
xmlSerializer.text(sms.getBody());
xmlSerializer.endTag(null,"body");
xmlSerializer.endTag(null,"sms");
}
xmlSerializer.endTag(null,"AllSms"); //内部自动将流关闭
xmlSerializer.endDocument(); //结束标签与startDocument对应
} catch (IOException e) {
e.printStackTrace();
} finally {
}
}
2.使用 XmlPullParser 解析XML文档:
private ArrayList<SmsBean> readXmlFromLocal(){
try {
File path = new File(Environment.getExternalStorageDirectory(),"sms.xml"); //指定要读取的文件的位置
InputStream in = new FileInputStream(path);//获取一个输入流
XmlPullParser xmlPullParser = Xml.newPullParser(); //创建一个xml的Pull解析器
xmlPullParser.setInput(in,"utf-8"); //设置读入的Xml的文件编码
int eventType = xmlPullParser.getEventType(); //获得事件类型( START_DOCUMENT)
ArrayList<SmsBean> BeanList = null;
SmsBean Bean = null;
while(eventType!=XmlPullParser.END_DOCUMENT){ //如果获得的事件类型是END_DOCUMENT就循环结束
String tagName = xmlPullParser.getName(); //获取当前节点的名字
switch (eventType){
case XmlPullParser.START_TAG: //判断类型如果等于开始的节点
if(tagName.equals("AllSms")){
BeanList = new ArrayList<SmsBean>();
}else if(tagName.equals("sms")){
Bean = new SmsBean();
}else if(tagName.equals("address")){
Bean.setAddress(xmlPullParser.nextText());
}else if(tagName.equals("date")){
Bean.setDate(xmlPullParser.nextText());
}else if(tagName.equals("body")){
Bean.setBody(xmlPullParser.nextText());
}
break;
case XmlPullParser.END_TAG:
if(tagName.equals("sms")){
BeanList.add(Bean);
}
break;
}
eventType = xmlPullParser.next(); //获得下一个事件类型
}
return BeanList;
} catch (XmlPullParserException e) {
e.printStackTrace();
}catch (IOException e){
e.printStackTrace();
}
finally {
}
return null;
}