I am parsing this xml. I want to get all the values from attributes but after searching a lot I am only able to fetch the first item value. Can anybody let me know how can I get all the id from all the items. I have pasted my code also
XML:
<query>
<item id='9173' name='A'/>
<item id='9174' name='B'/>
<item id='9175' name='C'/>
<item id='9176' name='D'/>
<item id='9174' name='E'/>
</query>
Code:
boolean done = false;
while (!done) {
int eventType = parser.next();
if (eventType == XmlPullParser.START_TAG) {
}
else if (eventType == XmlPullParser.END_TAG) {
Map<String,String> attributes = getAttributes(parser);
if (parser.getName().equals("query")) {
done = true;
}
}
}
private Map<String,String> getAttributes(XmlPullParser parser) throws Exception { Map<String,String> attrs=null;
int acount=parser.getAttributeCount();
if(acount != -1) {
Log.d(MY_DEBUG_TAG,"Attributes for ["+parser.getName()+"]");
attrs = new HashMap<String,String>(acount);
for(int x=0;x<acount;x++) {
Log.d(MY_DEBUG_TAG,"\t["+parser.getAttributeName(x)+"]=" +
"["+parser.getAttributeValue(x)+"]");
attrs.put(parser.getAttributeName(x), parser.getAttributeValue(x));
}
}
else {
throw new Exception("Required entity attributes missing");
}
return attrs;
}