metemet06
metemet06

Reputation: 1472

XmlPullParser getting all child values

How can I get child xml values ?

I mean : If xml.getName() = Section give me its Sub values. I think I must use a while loop in it but I couldnt achieve it !

OR is it possible to get parent value ?

<Top name="Top category">

  <Section name="Sub category 1">
     <Sub note="text" />
     <Sub note="text" />
  </Section>

<Section name="Sub category 2">
     <Sub note="text" />
     <Sub note="text" />
  </Section>

</Top>

...

  while (eventType != XmlPullParser.END_DOCUMENT) {

 if ((eventType == XmlPullParser.START_TAG) &&(_xml.getName().equals("Section"))){
                    String _Section = _xml.getAttributeValue(null, "name");

// take its Sub note values


   }    

 }

}

Upvotes: 0

Views: 1496

Answers (2)

rock_win
rock_win

Reputation: 755

May I suggest using a good de-serialization library like AQuery - https://code.google.com/p/android-query/

I'm no way associated with the project, but seems it can solve your task in hand much more efficiently and robustly

Upvotes: 0

dmon
dmon

Reputation: 30168

When dealing with pull parsers you have to create an object such that it can be accessed when you run into a subelement of that object. For example in this case whenever you run into a START_TAG for a Section, create a Section object and save it in a variable outside of the method scope. Then if you find another START_TAG Sub and the Section is not null,it means you're in a section and you need to add a sub section. Once you find an END_TAG for Section then add the current Section object to a list and then set that variable you had to null.

Upvotes: 1

Related Questions