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.

1
  • Would you consider a non pull based answer? Commented Apr 25, 2016 at 4:12

2 Answers 2

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;
Sign up to request clarification or add additional context in comments.

2 Comments

it was actually nothing. i juss messed up with it. thanks alot :)
actually i used flag thats y accepted othr ans. and commented on this :)
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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.