不得不改写一些方法,因为我的搭档是偏执狂,哈哈。如果您看到任何拼写错误,可能是因为这个,但如果您认为它们可能导致问题,请指出它们
使用 XmlPullParser 并在第 80 行,该值始终等于“通道”。当它调用 skip 时,parser.next() 总是等于 4。这段代码曾经可以工作(由于学校原因放在一边),现在它什么也不返回。它似乎正在跳过所有内部标签。
任何想法为什么?
更新:看起来我得到了一个 org.xmlpull.v1.XmlPullParserException。当我将异常打印到日志时,我得到以下结果。我认为这是说它正在寻找一个开始标签,但它正在返回 null(?):
07-17 00:02:14.564: E/XmlPullParserException(13604): org.xmlpull.v1.XmlPullParserException: expected: START_TAG {null}channel (position:START_TAG <rss xmlns:a10='http://www.w3.org/2005/Atom' version='2.0'>@2:60 in java.io.InputStreamReader@4259cca8)
这是解析器类:
import java.io.IOException;
import java.io.InputStream;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;
import android.util.Log;
import android.util.Xml;
public class Parser {
// XML node keys
private final String KEY_ITEM = "item"; // parent node
private final String KEY_GUID = "guid";
private final String KEY_LINK = "link";
private final String KEY_TITLE = "title";
private final String KEY_DESCRIPTION = "description";
private final String KEY_UPDATED = "updated";
public static final String nameSpace = null;
public void parse(InputStream in, String type)
throws XmlPullParserException, IOException {
try {
XmlPullParser parser = Xml.newPullParser();
parser.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES, false);
parser.setInput(in, null);
parser.nextTag();
readFeed(parser, type);
} finally {
in.close();
}
}
private void readFeed(XmlPullParser parser, String type)
throws XmlPullParserException, IOException {
parser.require(XmlPullParser.START_TAG, nameSpace, null);
while (parser.next() != XmlPullParser.END_TAG) {
if (parser.getEventType() != XmlPullParser.START_TAG) {
continue;
}
String name = parser.getName(); // This is line 80
Log.e("READFEED", String.valueOf(name));
if (name.equals(KEY_ITEM)) {
DatabaseManager.sInstance.addItem(readItem(parser, type));
} else {
skip(parser);
}
}
}
private Item readItem(XmlPullParser parser, String sportType)
throws XmlPullParserException, IOException {
parser.require(XmlPullParser.START_TAG, nameSpace, KEY_ITEM);
Log.e("readItem", "readItem");
long id = 0;
String link = null;
int linkType = 0;
String title = null;
String description = null;
String updated = null;
while (parser.next() != XmlPullParser.END_TAG) {
if (parser.getEventType() != XmlPullParser.START_TAG) {
continue;
}
String name = parser.getName();
Log.e("readItem", name);
if (name.equalsIgnoreCase(KEY_LINK)) {
link = readTag(parser, KEY_LINK);
if (link.contains("player")) {
linkType = 1;
}
} else if (name.equalsIgnoreCase(KEY_TITLE)) {
title = readTag(parser, KEY_TITLE);
} else if (name.equalsIgnoreCase(KEY_DESCRIPTION)) {
description = readTag(parser, KEY_DESCRIPTION);
} else if (name.equalsIgnoreCase(KEY_GUID)) {
id = Long.valueOf(readTag(parser, KEY_GUID).replace(" ", ""));
} else if (name.equalsIgnoreCase(KEY_UPDATED)) {
updated = readTag(parser, KEY_UPDATED);
Pattern regex = Pattern.compile("T*");
Matcher regexMatcher = regex.matcher(updated);
if (regexMatcher.find()) {
String date = regexMatcher.group();
}
} else {
skip(parser);
}
}
return DatabaseManager.sInstance.newItem(id, sportType, link, linkType, title,
description, updated);
}
// Processes description tags in the feed.
private String readTag(XmlPullParser parser, String tag)
throws IOException, XmlPullParserException {
parser.require(XmlPullParser.START_TAG, nameSpace, tag);
String text = readText(parser);
parser.require(XmlPullParser.END_TAG, nameSpace, tag);
return text;
}
// For the tags title and description, 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;
}
}
}
}
这是代码从中提取的 XML 的结构
<rss xmlns:a10="http://www.w3.org/2005/Atom" version="2.0">
<channel>
<title></title>
<link></link>
<description></description>
<language></language>
<lastBuildDate></lastBuildDate>
<item>
<guid isPermaLink="false"></guid>
<link></link>
<title></title>
<description></description>
<a10:updated></a10:updated>
</item>
<item>
<guid isPermaLink="false"></guid>
<link></link>
<title></title>
<description></description>
<a10:updated></a10:updated>
</item>
<item>
<guid isPermaLink="false"></guid>
<link></link>
<title></title>
<description></description>
<a10:updated></a10:updated>
</item>
<item>
<guid isPermaLink="false"></guid>
<link></link>
<title></title>
<description></description>
<a10:updated></a10:updated>
</item>
</channel>
</rss>
提前谢谢大家:)