引用在其他地方的一些话:
Pull简介
1.常见的XML解析方式有三种,DOM、SAX、Pull,Android系统中推荐使用Pull
2.Pull解析器是一个开源的Java项目,Android系统内部解析XML文件均为此种方式,也可用于JavaEE项目
3.Android SDK中已经集成了Pull解析器,无需添加任何jar文件
4.Pull解析器运行方式与SAX类似,提供各种事件的判断
5.官方网站:http://xmlpull.org/
使用Pull解析器解析XML文件
1. Xml.newPullParser() 获得解析器
2 parser.setInput(in,"UTF-8") 设置输入流以及编码
3. parser.next() 获取下一个解析事件,得到一个事件代码
4. XmlPullParser中定义了常量来标识各种解析事件
START_DOCUMENT、END_DOCUMENT 、START_TAG 、END_TAG 、TEXT
下面是个小案例:整个工程的结构大致如下:
待解析的XML文档
<?xml version="1.0" encoding="gb2312"?> <addr> <person id="001"> <name>Tom</name> <address>广州市天河区</address> <tel>020-85210001</tel> <Company>华南师范大学</Company> </person> <person id="002"> <name>Bob</name> <address>广州市天河区</address> <tel>020-85210002</tel> <Company>华南师范大学</Company> </person> <person id="003"> <name>Alice</name> <address>广州市天河区</address> <tel>020-85210003</tel> <Company>华南师范大学</Company> </person> </addr>
package com.example.domain; public class AddrInfos { private int id; private String name; private String Address; private String tel; private String Company; 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 String getAddress() { return Address; } public void setAddress(String address) { Address = address; } public String getTel() { return tel; } public void setTel(String tel) { this.tel = tel; } public String getCompany() { return Company; } public void setCompany(String company) { Company = company; } @Override public String toString() { return "AddrInfos [人物id=" + id + ",姓名=" + name + ",住址=" + Address + ",电话=" + tel + ",组织=" + Company + "]"; } }
package com.example.InformationServices; import java.io.InputStream; import java.util.ArrayList; import java.util.List; import org.xmlpull.v1.XmlPullParser; import android.util.Xml; import com.example.domain.AddrInfos; public class InformationServices { public static List<AddrInfos> getAddrInfos(InputStream is) throws Exception { XmlPullParser parser = Xml.newPullParser(); // 初始化解析器 parser.setInput(is,"UTF-8"); // 获取事件的类型 int type = parser.getEventType(); List<AddrInfos> list = null; AddrInfos addr = null; while (type != XmlPullParser.END_DOCUMENT) { switch (type) { case XmlPullParser.START_TAG: if ("addr".equals(parser.getName())) { // 解析到全局开始标签 list = new ArrayList<AddrInfos>(); } // 解析到“person”标签 else if ("person".equals(parser.getName())) { addr = new AddrInfos(); String id = parser.getAttributeValue(0); addr.setId(Integer.parseInt(id)); } // 解析到“name”标签 else if ("name".equals(parser.getName())) { String name = parser.nextText(); addr.setName(name); } // 解析到“address”标签 else if ("address".equals(parser.getName())) { String address = parser.nextText(); addr.setAddress(address); } // 解析到“tel”标签 else if ("tel".equals(parser.getName())) { String tel = parser.nextText(); addr.setTel(tel); } // 解析到“Company”标签 else if ("Company".equals(parser.getName())) { String Company = parser.nextText(); addr.setCompany(Company); } break; case XmlPullParser.END_TAG: if ("person".equals(parser.getName())) { // 一个人的信息处理完毕 list.add(addr); addr = null; } break; } // 可以循环终止条件 type = parser.next(); } return list; } }
MainActivity主界面
package com.example.xmlresolve; import java.util.List; import com.example.InformationServices.InformationServices; import com.example.domain.AddrInfos; import android.app.Activity; import android.os.Bundle; import android.util.Log; import android.widget.TextView; import android.widget.Toast; public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); TextView tv = (TextView) findViewById(R.id.tv); StringBuilder sb=new StringBuilder(); try { List<AddrInfos> infos= InformationServices.getAddrInfos(MainActivity.class.getClassLoader() .getResourceAsStream("Address_Book.xml")); for(AddrInfos info:infos){ String str=info.toString(); sb.append(str); sb.append("\n"); } Log.i("addr","輸出"); tv.setText(sb.toString()); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); Toast.makeText(this,"解析失败",0).show(); } }
效果如图: