0

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 :-)

5
  • 2
    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 : w3schools.com/xpath/xpath_syntax.asp Try : "/Quiz/Inhalt[@number=2]@Frage" You will get test2 Commented Oct 18, 2012 at 14:13
  • Thank you, but When i try this, i get the error: Extra illegal tokens '@','Frage' Commented Oct 18, 2012 at 15:17
  • 1
    it worked this way:XPathExpression expr= xpath.compile("//Inhalt[@nummer='2']/@Frage"); Commented Oct 18, 2012 at 16:24
  • Right... there was a / missing before the @ in @Stephane's suggestion. In regard to learning XPath: please don't use w3schools, unless you want to have to unlearn their mistakes later. (See w3fools.com) Instead try zvon.org/xxl/XPathTutorial/General/examples.html Commented Oct 18, 2012 at 17:39
  • @StephaneMathis, could you make your comment into an answer? Then, this question will no longer be categorized as "unanswered", and Basem can "accept" your answer if he wants to. (And you will likely get more rep.) Commented Oct 18, 2012 at 17:41

1 Answer 1

1

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.