本文介绍了XmlResourceParser.getText()返回null的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个名为xml
的XmlResourceParser
实例.如我的代码所示,当我尝试在节点上调用getText()
时,它返回null.这很奇怪,因为我可以在返回正确值的同一节点上调用getName()
,因此实例已正确设置.这是我的代码:
I have an XmlResourceParser
instance called xml
. When I try to call getText()
on a node, as seen in my code, it returns null. This is strange because I can call getName()
on the same node it returns the proper value, so the instance is set up properly. Here is my code:
XmlResourceParser xml = context.getResources().getXml(R.xml.thesaurus);
try {
//if (xml.getName().equals("word")) {
xml.next(); //to the first node within <word></word>
boolean notFound = true;
while (notFound) {
xml.next();
if (xml.getName() != null && xml.getName().equalsIgnoreCase("synonyms")) {
String synonym = xml.getText();
Log.v(TAG, String.valueOf(synonym));
notFound = false; //found
}
}
}
} catch (XmlPullParserException xppe) {
xppe.printStackTrace();
} catch (IOException ioe) {
ioe.printStackTrace();
}
这是我的XML,即使它没有任何问题:
Here is my XML, even though there isn't anything wrong with it:
<?xml version="1.0"?>
<thesaurus>
<word name="let">
<synonyms>allow</synonyms>
</word>
</thesaurus>
任何帮助将不胜感激!谢谢!
Any help would be appreciated! Thanks!
推荐答案
我找到了自己的答案在这里.我使用了@Libin在此答案中发布的代码.
I found my own answer here. I used the code posted in this answer by @Libin.
int eventType = xmlResourceParser.getEventType();
while (eventType != XmlPullParser.END_DOCUMENT) {
if (eventType == XmlPullParser.START_DOCUMENT) {
System.out.println("Start document");
} else if (eventType == XmlPullParser.START_TAG) {
System.out.println("Start tag " + xmlResourceParser.getName());
} else if (eventType == XmlPullParser.END_TAG) {
System.out.println("End tag " + xmlResourceParser.getName());
} else if (eventType == XmlPullParser.TEXT) {
System.out.println("Text " + xmlResourceParser.getText());
}
eventType = xmlResourceParser.next();
}
感谢大家的帮助
这篇关于XmlResourceParser.getText()返回null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!