Below is the code which displays all the xml. I don’t want to display all the xml content but only want to check if the id = 2 than display the description. Please help me modify the code.
private String getEventsFromAnXML(Activity activity)throws XmlPullParserException, IOException
{
StringBuffer stringBuffer = new StringBuffer();
Resources res = activity.getResources();
XmlResourceParser xrp = res.getXml(R.xml.books.xml);
xrp.next();
int eventType = xrp.getEventType();
while (eventType != XmlPullParser.END_DOCUMENT)
{
if(eventType == XmlPullParser.START_DOCUMENT)
{
stringBuffer.append(" ");
}
if(eventType == XmlPullParser.START_TAG)
{
stringBuffer.append("First Tag " + xrp.getName());
}
else if(eventType == XmlPullParser.TEXT)
{
stringBuffer.append("Second Tag "+xrp.getText());
}
else if(eventType == XmlPullParser.END_TAG)
{
stringBuffer.append("Third Tag "+xrp.getName());
}
eventType = xrp.next();
}
stringBuffer.append("\n--- End XML ---");
return stringBuffer.toString();
}
my xml file
<?xml version="1.0" encoding="utf-8" ?>
<Books>
<Number id ="1">
<Description>This is science book.
</Description>
</Number>
<Number id = "2">
<Description>This is about cooking.
</Description>
</Number>
</Books>
Heer is the code…..