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();
}
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!
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();
}
Thanks for everyone's help