1

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.

Community
  • 1
  • 1
Romi
  • 4,833
  • 28
  • 81
  • 113

2 Answers2

4

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;
Paresh Mayani
  • 127,700
  • 71
  • 241
  • 295
2

You can use a FLAG and initialize its value to false. When first you enter the name tag check for false and when you get in the tag then set its value to true. Then it will not enter again.

Mohit Verma
  • 3,025
  • 1
  • 20
  • 36