简体   繁体   中英

Xml parsing in Android, Null exception

i have an xml that looks this way

<Quiz>
<Inhalt Frage="test" number="1" />
<Inhalt Frage ="test2" number=2"/> 
</Quiz>

Now, in android i want to parse this xml in that way, that the result show me the content of "Frage" for the number 2. I have tried to parse it this way, but i always get a nullexception error:

XPathFactory factory = XPathFactory.newInstance();
XPath xpath = factory.newXPath();
XPathExpression expr= xpath.compile("//Frage[@number=2]/text()");

Object result = expr.evaluate(doc, XPathConstants.NODESET);
NodeList nodes = (NodeList) result;
einga= nodes.item(0).getNodeName();

Would be great, if someone could help me with this. Thank you :-)

You are trying to select the text from the node Frage which have an attribute number with the value 2. Are you familiar with Xpath expressions? Try this link : http://w3schools.com/xpath/xpath_syntax.asp

Try : "//Inhalt[@number='2']/@Frage" . You will get test2

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM