Parse xml using XmlPullParser

Romi picture Romi · Nov 5, 2011 · Viewed 7.3k times · Source

i am working with android and using xmlpullparser in android to parse an xml document that is like:

<spaces>
  <space>
    <name></name>
    <desc></desc>
    <songs>
      <song>
        <name></name>
        <thumb></thumb>
      </song>
      <song>
        <name></name>
        <thumb></thumb>
      </song>
    </songs>
  </space>
</spaces>

am using this:

public List<Space> parse(String xmlString) {
        List<Space> Spaces = null;
        XmlPullParser parser = Xml.newPullParser();
        try {
            parser.setInput(new StringReader(xmlString));
            int eventType = parser.getEventType();
            Space currentSpace = null;
            boolean done = false;
            while (eventType != XmlPullParser.END_DOCUMENT && !done) {
                String name = null;
                switch (eventType) {
                case XmlPullParser.START_DOCUMENT:
                    Spaces = new ArrayList<Space>();
                    break;
                case XmlPullParser.START_TAG:
                    name = parser.getName();
                    if (name.equalsIgnoreCase("space")) {
                        currentSpace = new Space();
                        System.out.println("space");

                    } else if (currentSpace != null) {

                        if (name.equalsIgnoreCase("name")) {
                            currentSpace.setName(parser.nextText());
                            System.out.println(":::"+currentSpace.getName());

                        } else if (name.equalsIgnoreCase("id")) {
                            if (currentSpace.getId() == null) {
                                currentSpace.setId(parser.nextText());
                            }

                        }

                    }

                    break;
                case XmlPullParser.END_TAG:
                    name = parser.getName();
                    if (name.equalsIgnoreCase("space") && currentSpace != null) {
                        Spaces.add(currentSpace);

                    } else if (name.equalsIgnoreCase("spaces")) {
                        done = true;

                    }
                    break;
                }
                eventType = parser.next();  
            }
        } catch (Exception e) {
            Log.e("Projects List", e.getMessage(), e);
            throw new RuntimeException(e);
        }
        return Spaces;
    }

My problem is i want only <name></name> which is in <space> not within <song> and using above code am getting both.

i looked Parsing XML XmlPullParser android but dint get my solution.

Please Reply. Thanks.

Answer

Paresh Mayani picture Paresh Mayani · Nov 5, 2011

while parsing START_TAG, when you parse <space> tag, take one counter variable and assign it 1 and then whenever you get <name> tag, at that increment it by 1.

Now, while parsing <name> tag, just check whether <name> tag comes for the 1st time, if it is then parse the desired value.

case XmlPullParser.START_TAG:
                    name = parser.getName();
                    if (name.equalsIgnoreCase("space")) {
                        cnt=1;
                        currentSpace = new Space();
                        System.out.println("space");

                    } else if (currentSpace != null) {

                        if (name.equalsIgnoreCase("name")) 
                         {
                            currentSpace.setName(parser.nextText());
                            if(cnt==1)
                            {
                                // you have <name> from <space>
                            }
                            System.out.println(":::"+currentSpace.getName());
                            cnt++;
                        } else if (name.equalsIgnoreCase("id")) {
                            if (currentSpace.getId() == null) {
                                currentSpace.setId(parser.nextText());
                            }

                        }

                    }

                    break;