Android XmlPullParser to pull unknown number of items nested deeper

KatoS1997

New Member
I have the following XML hierarchy:\[code\]<?xml version="1.0" encoding="UTF-8" standalone="yes"?><recepies xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"><recepie name="recepie1"> <item></item> <NumberOfPersons>1</NumberOfPersons> <Ingredients> <string></string> <string></string> </Ingredients> <Preparation></Preparation> <Energy>342</Energy> <Protein>8</Protein> <RecipeCategory>3</RecipeCategory></recepie>\[/code\]Everything is fixed, but my "Ingredients" are variable, and I can have many or one.\[code\]try { XmlPullParser xpp=getResources().getXml(R.xml.recipes); while (xpp.getEventType()!=XmlPullParser.END_DOCUMENT) { if (xpp.getEventType()==XmlPullParser.START_TAG) { if (xpp.getName().equals("recepie")) { items.add(xpp.getAttributeValue(0)); Log.w("Valjda citam iz XMLa",xpp.getAttributeValue(0)); } if (xpp.getName().equalsIgnoreCase("item")) { //items.add(xpp.getProperty(name)); Log.w("Item",xpp.nextText()); } else if (xpp.getName().equalsIgnoreCase("NumberOfPersons")) { //items.add(xpp.getAttributeValue(0)); Log.w("NumberOfPersons",xpp.nextText()); } else if (xpp.getName().equalsIgnoreCase("Preparation")) { //items.add(xpp.getAttributeValue(0)); Log.w("Preparation",xpp.nextText()); } else if (xpp.getName().equalsIgnoreCase("Energy")) { //items.add(xpp.getAttributeValue(0)); Log.w("Energy",xpp.nextText()); } else if (xpp.getName().equalsIgnoreCase("Protein")) { //items.add(xpp.getAttributeValue(0)); Log.w("Protein",xpp.nextText()); } else if (xpp.getName().equalsIgnoreCase("RecipeCategory")) { //items.add(xpp.getAttributeValue(0)); Log.w("RecipeCategory",xpp.nextText()); } } while (xpp.getEventType()!=XmlPullParser.END_DOCUMENT) { if (xpp.getEventType()==XmlPullParser.START_TAG) { if (xpp.getName().equals("Ingredients")) { items.add(xpp.getAttributeValue(0)); Log.w("Valjda citam iz XMLa",xpp.getAttributeValue(0)); } if (xpp.getName().equalsIgnoreCase("string")) { //items.add(xpp.getProperty(name)); Log.w("Item",xpp.nextText()); } } } xpp.next(); } } catch (Throwable t) { Toast .makeText(this, "Request failed: "+t.toString(), Toast.LENGTH_LONG) .show(); }\[/code\]Now, I'm trying to read my recipe, and fill the recipe object (Here it's just Log output for the ease of use), and when it comes to "Ingredients", I'd love to pass through Ingredients, and fill ingredients object. But, It seems I have a problem understanding pullParser when it comes to deeper nested elements.I assume I'd need to do code similar to one on the beginning, and loop while there are elements of ingredients, but I'm missing something to connect it properly. Any help would be appreciated.Tnx.
 
Back
Top