title: How to parse a local XML file in Android?
body: <p>I want to retrieve data from an xml file in android app. Below is the code I am working with. I think the if statements I am using are not working well ( they are not picking the description to display). I want to display the text(description) if the id value is for example 2. Please review the code and help me correct it.Thanks.</p>
  
  
  
  <pre class="lang-xml prettyprint-override"><code>&lt;?xml version="1.0" encoding="utf-8" ?&gt; 
  &lt;Books&gt; 
    &lt;Number id ="1"&gt; 
      &lt;Description&gt;This text is for id whose value is equal to 1. &lt;/Description&gt; 
    &lt;/Number&gt; 
    &lt;Number id = "2"&gt; 
      &lt;Description&gt;This text is for id whose value is equal to 2. &lt;/Description&gt; 
    &lt;/Number&gt; 
  &lt;/Books&gt; 
  </code></pre>
  
  
  
  <pre class="lang-java prettyprint-override"><code>String stringXmlContent;
  try {
      stringXmlContent = getEventsFromAnXML(this);
      tv.setText(stringXmlContent);
  } catch (XmlPullParserException e) {
      e.printStackTrace();
  } catch (IOException e) {
      e.printStackTrace();
  }
  
  button1.setOnClickListener(this);
  
  private String getEventsFromAnXML(Activity activity)throws XmlPullParserException, IOException
  {
       StringBuffer stringBuffer = new StringBuffer();
       String attVal = null;
       String desc;
       Resources res = activity.getResources();
       XmlResourceParser xrp = res.getXml(R.xml.myxml);
       try {
              xrp.next();
              int eventType = xrp.getEventType();
              while (eventType != XmlPullParser.END_DOCUMENT)
              {
                  if(eventType == XmlPullParser.START_DOCUMENT)
                  {
                      stringBuffer.append(" ");
                  }
                  if(eventType == XmlPullParser.START_TAG)
                  {    
                    if(xrp.getName().equals("Number")){    
                       attVal =  xrp.getAttributeValue(0); 
                    }       
                  }
                  else if(eventType == XmlPullParser.TEXT)
                  {    
                     if(xrp.getName().equals("Description") &amp;&amp; attVal.equals("2")){
                     stringBuffer.append(" " + xrp.getText());
                  }   
  
              }
  
      } catch (XmlPullParserException e) {
          // TODO Auto-generated catch block
          e.printStackTrace();
      } catch (IOException e) {
          // TODO Auto-generated catch block
          e.printStackTrace();
      }
  
      return stringBuffer.toString();  
  }
  </code></pre>