我的代码类似于
public void parse(String input) throws XmlPullParserException, IOException {
InputStream is = null;
try {
is = new ByteArrayInputStream(input.getBytes("Big-5"));
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
XmlPullParser parser = Xml.newPullParser();
parser.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES,false);
parser.setInput(is,"Big-5");
readFeed(parser);
}
private void readFeed(XmlPullParser parser) throws XmlPullParserException, IOException {
int eventType = parser.getEventType();
while(eventType!=parser.END_DOCUMENT){
String text = parser.getName();
Log.d(TAG,"text:"+text);
eventType=parser.nextTag();
}
}
logcat 有错误
01-20 18:41:34.361 17574-17574/global.fantasyfighter.hongkongnews W/System.err﹕ org.xmlpull.v1.XmlPullParserException: expected: /SCRIPT read: script (position:END_TAG </script>@23:10 in java.io.InputStreamReader@428c5000)
Xml 的前几行:
<meta http-equiv="refresh" content="900" />
<link rel="image_src" href="http://www.hkheadline.com/images/headline_logo_2_2_line_small.jpg">
<SCRIPT LANGUAGE="JavaScript">
<!-- somefunction omitted by author-->
</script>
从这个错误中我知道这是因为 XmlPullParser find <SCRIPT>
and expect</SCRIPT>
在 xml 中找到,而是</script>
找到了。问题是我无法在解析期间使 XmlPullParser 对标记不区分大小写。XmlPullParserException 一直在我的应用程序中抛出。