问题
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!
回答1:
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
回答2:
When you call xml.getText() the xml parser currently points at the START_TAG, and not at the content. Calling xml.next() lets getText() return the content:
if (xml.getName() != null && xml.getName().equalsIgnoreCase("synonyms")) {
xml.next();
String synonym = xml.getText();
Log.v(TAG, String.valueOf(synonym));
notFound = false; //found
}
You can verify the iterator position for example with:
if (xml.getEventType() == XmlPullParser.TEXT) {
// iterator is at content
}
回答3:
try this
final String xml ="<?xml version=\"1.0\"?><thesaurus><word name=\"let\"><synonyms>allow</synonyms></word></thesaurus>";
final DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = null;
try {
builder = builderFactory.newDocumentBuilder();
final Document xmlDocument = builder.parse(new ByteArrayInputStream(xml.getBytes()));
final XPath xPath = XPathFactory.newInstance().newXPath();
final Object result = xPath.compile("/thesaurus/word/synonyms").evaluate(xmlDocument, XPathConstants.NODESET);
NodeList nodes = (NodeList) result;
for (int h = 0; h < nodes.getLength(); h++) {
final Node node = nodes.item(h);
final NodeList venueChildNodes = node.getChildNodes();
System.out.println(node.getChildNodes().item(0).getTextContent());
}
} catch (ParserConfigurationException | SAXException | IOException e) {
e.printStackTrace();
} catch (XPathExpressionException e) {
e.printStackTrace();
}
来源:https://stackoverflow.com/questions/32685464/xmlresourceparser-gettext-returning-null