0
問題:XML解析返回空指針異常,無法從xml中的程序標記中檢索文本。步執行代碼表示: (這是從調試語句如示於下面的代碼)Android XML解析(XmlPullParser) - 無法從XML中檢索文本
err StartTag entry
err StartTag record
err Text 4
線調用解析器(也導致空指針的一個,桶是一個新創建的容器):
pail = XmlParsee.parsee(getResources().openRawResource(R.raw.testprogramlist));
我的XML文件:
<entry>
<record>
<program>Program 1(English)</program>
</record>
<record>
<program>Program 2(Mandarin)</program>
</record>
</entry>
我解析器(我都嘗試的getText()和nextText(),都返回了同樣的問題):
public class XmlParsee {
// jealousy has invaded! Generic xml parser that returns a container
public static Container parsee(InputStream inputriver) {
Container container = null;
try {
// get new parser object from factory
XmlPullParser parsee = XmlPullParserFactory.newInstance().newPullParser();
parsee.setInput(inputriver, null);
int eventType = parsee.getEventType();
// while xml still has more, we read.
while (eventType != XmlPullParser.END_DOCUMENT) {
switch (eventType) {
case XmlPullParser.START_DOCUMENT: {
//these comments to be replaced with logging functions, if you desire
//wokay, we begin
container = new Container();
System.err.println("doc start");
break;
}
case XmlPullParser.END_DOCUMENT:{
//wokay, we end.
break;
}
case XmlPullParser.START_TAG:{
//new tag! first we get the tag's name
String tag = parsee.getName();
System.err.println("StartTag "+parsee.getName());
//then we check, individually, what the tag is to confirm content
//if <program>
if(tag.equalsIgnoreCase(Container.PROGRAM)){
System.err.println("Text "+parsee.TEXT);
//container.addProgramList(parsee.getText());
}
//if <>
break;
}
}
//done with this line, next!
eventType = parsee.next();
}
} catch (Exception e) {
container = null;
}
return container;
}
任何想法?自從早上起,我一直在抨擊我的頭像= \
好了,是我不好。找出錯誤的地方。 現在感覺很愚蠢。謝謝! – user1667600