How to choose a particular element from XML file?

There is a XML file voprosi.xml
<?xml version="1.0" encoding="utf-8"?>
<questions>
<question>
 <issue>what is 2+2?</issue>
<otvet_1>2</otvet_1>
<otvet_2>6</otvet_2>
<otvet_3>8</otvet_3>
 <otvet_4 name="pravilno">4</otvet_4>
</question>
<question>
 <issue>what is 3+3?</issue>
<otvet_1>12</otvet_1>
<otvet_2>16</otvet_2>
<otvet_3>18</otvet_3>
 <otvet_4 name="pravilno">6</otvet_4>
</question>
<question>
 <issue>How much is 4+4?</issue>
<otvet_1>22</otvet_1>
<otvet_2>26</otvet_2>
<otvet_3>18</otvet_3>
 <otvet_4 name="pravilno">8</otvet_4>
</question>
<question>
 <issue>How much is 5+5?</issue>
<otvet_1>2</otvet_1>
<otvet_2>6</otvet_2>
<otvet_3>8</otvet_3>
 <otvet_4 name="pravilno">10</otvet_4>
</question>
</questions>

I have to get in aktiviti Question 3 and the answer choices

Java code that retrieves data from XML file

try {
 XmlPullParser parser = getResources().getXml(R. xml.voprosi);

 while (parser.getEventType() != XmlPullParser.END_DOCUMENT) {
 if (parser.getEventType() == XmlPullParser.START_TAG
 && parser.getName().equals("question")) {

if(numvopr==3)
{

//....... here it is necessary to bring out both the question and the answers 

}
numvopr++;
}
parser.next();
}


 } catch (Throwable t) {
Toast.makeText(this,
 "Error loading XML document:" + t.toString(),
Toast.LENGTH_LONG).show();
 }


Perhaps you can output the data so as not to parse the whole file but only the item number?
March 19th 20 at 08:29
1 answer
March 19th 20 at 08:31
Solution
with a strong desire - you can if you go to the block structure of the file,
i.e. when each tag <question> is strictly N bytes (the remainder can be supplemented by spaces)
then the desired block is in one step: the block number*N
but in a real project for that will beat not only the hands of time
there is a parser, there are objects, aspartyl - received, the decision is valuable for its predictability and ease of maintenance
PS: the tags of the form<otvet_1>12</otvet_1>better not to show anyone
so is correct:
<question id="1" text="what is 4+4?" > - make the question attributes to simplify parsing
<item id="1" right="true">8</item>
<item id="2" >44</item>

Find more questions by tags XMLAndroid