最近在学习android 的网络编程,其中有一部分很重要的就是如何解析xml文件,android 平台下推荐的解析器是
XmlPullParser,官方文档称该解析器是集高效与可维护性的解析器。原文如下:
We recommend XmlPullParser, which is an efficient and maintainable way to parse XML on Android.
下午去官方那里研究了一下它给的教程,发现这个玩意用起来确实挺简单的,对于解析一些简单常用的xml文件,确实帮助很大。下面我就官方的教程来深入浅出一下这个XmlPullParser 下面,我们尝试者来解析类似与下面的一个xml文件。
- <?xml version="1.0" encoding="utf-8"?>
- <feed>
- <entry>
- <title type="text">Where is my data file?</title>
- <link rel="alternate" href="" />
- <summary type="html">
- I have an Application that requires a data file..
- </summary>
- </entry>
- </feed>
如上所示:
首先,我们要先初始化一个解析器,parser,然后指定其开始的标签
- public class StackOverflowXmlParser {
- // We don't use namespaces
- private static final String ns = null;
-
- public List parse(InputStream in) throws XmlPullParserException, IOException {
- try {
- XmlPullParser parser = Xml.newPullParser(); //创建一个xml 的解析器
- parser.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES, false); //指定命名空间
- parser.setInput(in, null); //设置输入的文件流
- parser.nextTag();
- return readFeed(parser); //开始读取xml文件
- } finally {
- in.close();
- }
- }
- ...
- }
开始读取xml里面的实体内容,并把他们放入一个list里面
- private List readFeed(XmlPullParser parser) throws XmlPullParserException, IOException {
- List entries = new ArrayList();
- parser.require(XmlPullParser.START_TAG, ns, "feed"); //feed是根标签,只有一个
- while (parser.next() != XmlPullParser.END_TAG) {
- if (parser.getEventType() != XmlPullParser.START_TAG) {
- continue;
- }
- String name = parser.getName();
- // Starts by looking for the entry tag
- if (name.equals("entry")) { //entry 我们定义的实体的信息的开始标签
- entries.add(readEntry(parser));
- } else {
- skip(parser); //对于不敢兴趣的标签,跳过
- }
- }
- return entries;
- }
下面就对你感兴趣的标签,分别定义方法去读取他们。
首先定义一个代表xml里面实体的标签的类
public static class Entry {
public final String title;
public final String link;
public final String summary;
private Entry(String title, String summary, String link) {
this.title = title;
this.summary = summary;
this.link = link;
}
}
然后定义一个读取这个实体的方法,里面对应了读取各个字标签的方法。
- // Parses the contents of an entry. If it encounters a title, summary, or link tag, hands them off
- // to their respective "read" methods for processing. Otherwise, skips the tag.
- private Entry readEntry(XmlPullParser parser) throws XmlPullParserException, IOException {
- parser.require(XmlPullParser.START_TAG, ns, "entry");
- String title = null;
- String summary = null;
- String link = null;
- while (parser.next() != XmlPullParser.END_TAG) {
- if (parser.getEventType() != XmlPullParser.START_TAG) {
- continue;
- }
- String name = parser.getName();
- if (name.equals("title")) {
- title = readTitle(parser);
- } else if (name.equals("summary")) {
- summary = readSummary(parser);
- } else if (name.equals("link")) {
- link = readLink(parser);
- } else {
- skip(parser);
- }
- }
- return new Entry(title, summary, link);
- }
- // Processes title tags in the feed.
- private String readTitle(XmlPullParser parser) throws IOException, XmlPullParserException {
- parser.require(XmlPullParser.START_TAG, ns, "title");
- String title = readText(parser);
- parser.require(XmlPullParser.END_TAG, ns, "title");
- return title;
- }
-
- // Processes link tags in the feed.
- private String readLink(XmlPullParser parser) throws IOException, XmlPullParserException {
- String link = "";
- parser.require(XmlPullParser.START_TAG, ns, "link");
- String tag = parser.getName();
- String relType = parser.getAttributeValue(null, "rel");
- if (tag.equals("link")) {
- if (relType.equals("alternate")){
- link = parser.getAttributeValue(null, "href");
- parser.nextTag();
- }
- }
- parser.require(XmlPullParser.END_TAG, ns, "link"); //这里是设置它的结束标签
- return link;
- }
- // Processes summary tags in the feed.
- private String readSummary(XmlPullParser parser) throws IOException, XmlPullParserException {
- parser.require(XmlPullParser.START_TAG, ns, "summary");
- String summary = readText(parser);
- parser.require(XmlPullParser.END_TAG, ns, "summary");
- return summary;
- }
- // For the tags title and summary, extracts their text values.
- private String readText(XmlPullParser parser) throws IOException, XmlPullParserException {
- String result = "";
- if (parser.next() == XmlPullParser.TEXT) {
- result = parser.getText(); //读取文本内容
- parser.nextTag();
- }
- return result;
- }
- ...
- }
跳过你不感到兴趣的标签。
- private void skip(XmlPullParser parser) throws XmlPullParserException, IOException {
- if (parser.getEventType() != XmlPullParser.START_TAG) {
- throw new IllegalStateException();
- }
- int depth = 1;
- while (depth != 0) {
- switch (parser.next()) {
- case XmlPullParser.END_TAG:
- depth--;
- break;
- case XmlPullParser.START_TAG:
- depth++;
- break;
- }
- }
- }
注意到,其实用
XmlPullParser解析xml文件的时候,如果文件里面的格式有一点错时,就会发生异常,
但是,用它来解析xml文件,确实轻量级的,也是简单的,容易上手的,很适合手机平台。
阅读(4537) | 评论(0) | 转发(0) |