title: Parse xml using XmlPullParser
body: <p>i am working with android and using xmlpullparser in android to parse an xml document that is like:</p>
  
  <pre><code>&lt;spaces&gt;
    &lt;space&gt;
      &lt;name&gt;&lt;/name&gt;
      &lt;desc&gt;&lt;/desc&gt;
      &lt;songs&gt;
        &lt;song&gt;
          &lt;name&gt;&lt;/name&gt;
          &lt;thumb&gt;&lt;/thumb&gt;
        &lt;/song&gt;
        &lt;song&gt;
          &lt;name&gt;&lt;/name&gt;
          &lt;thumb&gt;&lt;/thumb&gt;
        &lt;/song&gt;
      &lt;/songs&gt;
    &lt;/space&gt;
  &lt;/spaces&gt;
  </code></pre>
  
  <p>am using this:</p>
  
  <pre><code>public List&lt;Space&gt; parse(String xmlString) {
          List&lt;Space&gt; 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 &amp;&amp; !done) {
                  String name = null;
                  switch (eventType) {
                  case XmlPullParser.START_DOCUMENT:
                      Spaces = new ArrayList&lt;Space&gt;();
                      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") &amp;&amp; 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;
      }
  </code></pre>
  
  <p>My problem is i want only <code>&lt;name&gt;&lt;/name&gt;</code> which is in <code>&lt;space&gt;</code> not within <code>&lt;song&gt;</code> and using above code am getting both.</p>
  
  <p>i looked <a href="https://stackoverflow.com/questions/7291886/parsing-xml-xmlpullparser-android">Parsing XML XmlPullParser android</a> but dint get my solution.</p>
  
  <p>Please Reply.
  Thanks.</p>