title: XML Parsing in Android
body: <p>After some operation i get the bewlow xml response from the server. Now i need to get the data from these tags. Please guide me how can i get that. </p>
  
  <pre><code>   &lt;?xml version="1.0" encoding="utf-8"?&gt;
      &lt;Response xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://tempuri.org/"&gt;
      &lt;Result xmlns="GlobalPayments"&gt;0&lt;/Result&gt;
      &lt;RespMSG xmlns="GlobalPayments"&gt;Approved&lt;/RespMSG&gt;
      &lt;Message xmlns="GlobalPayments"&gt;AP&lt;/Message&gt;
      &lt;AuthCode xmlns="GlobalPayments"&gt;01245F&lt;/AuthCode&gt;
      &lt;PNRef xmlns="GlobalPayments"&gt;170015964&lt;/PNRef&gt;
      &lt;HostCode xmlns="GlobalPayments"&gt;002&lt;/HostCode&gt;
      &lt;GetCVResultTXT xmlns="GlobalPayments"&gt;Service Not Requested&lt;/GetCVResultTXT&gt;
      &lt;GetCommercialCard xmlns="GlobalPayments"&gt;True&lt;/GetCommercialCard&gt;
      &lt;ExtData xmlns="GlobalPayments"&gt;CardType=Visa,BatchNum=0202&amp;lt;BatchNum&amp;gt;0202&amp;lt;/BatchNum&amp;gt;&amp;lt;ReceiptData&amp;gt;&amp;lt;MID&amp;gt;87228799&amp;lt;/MID&amp;gt;&amp;lt;Trans_Id&amp;gt;283220167525&amp;lt;/Trans_Id&amp;gt;&amp;lt;Val_Code&amp;gt;52ND&amp;lt;/Val_Code&amp;gt;&amp;lt;/ReceiptData&amp;gt;&lt;/ExtData&gt;
  
  &lt;/Response&gt;
  </code></pre>
  
  <p>I am using the below code and the Issue is the  <strong>XmlPullParser.START_TAG:</strong> gives the whole tags available into the xml not required one tag. </p>
  
  <pre><code> package com.parsing_new;
  
      import java.io.IOException;
      import java.io.InputStream;
      import java.util.ArrayList;
      import java.util.Iterator;
      import org.xmlpull.v1.XmlPullParser;
      import org.xmlpull.v1.XmlPullParserException;
      import org.xmlpull.v1.XmlPullParserFactory;
  
      import android.os.Bundle;
  
  import android.app.Activity;
  import android.util.Log;
  import android.view.Menu;
  import android.widget.TextView;
  
  class Response
  {
  
   public String Reslt;
   public String Respmsg;
   public String Msg;
   public String Auth;
   public String Pnref;
   public String Host;
   public String Getcv;
   public String Getcom;
   public String Ext;
  
  
  }
  public class ParsingActivity extends Activity {
   @Override
   protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_parsing);
    XmlPullParserFactory pullParserFactory;
    try {
     pullParserFactory = XmlPullParserFactory.newInstance();
     XmlPullParser parser = pullParserFactory.newPullParser();
         InputStream in_s = getApplicationContext().getAssets().open("temp.xml");
            parser.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES, false);
               parser.setInput(in_s, null);
               parseXML(parser);
    } catch (XmlPullParserException e) {
     e.printStackTrace();
    } catch (IOException e) {
     // TODO Auto-generated catch block
     e.printStackTrace();
    }
   }
   private void parseXML(XmlPullParser parser) throws XmlPullParserException,IOException
   {
    ArrayList&lt;Response&gt; responses = null;
          int eventType = parser.getEventType();
          Response currentProduct = null;
          while (eventType != XmlPullParser.END_DOCUMENT){
             String name = null;
              switch (eventType){
                  case XmlPullParser.START_DOCUMENT:
                   responses = new ArrayList&lt;Response&gt;();
                      break;
                  case XmlPullParser.START_TAG:
                      name = parser.getName();
                      Log.d("Result", name);
  
                      if (name == "Response"){
                          currentProduct = new Response();
                      }  else if (currentProduct != null){
                          if (name == "Result"){
                              currentProduct.Reslt = parser.nextText();
                          } else if (name == "RespMSG"){
                           currentProduct.Respmsg = parser.nextText();
                          } else if (name == "Message"){
                              currentProduct.Msg= parser.nextText();
                          }
                              else if (name == "AuthCode"){
                               currentProduct.Auth = parser.nextText();
                          }
                              else if (name == "PNRef "){
                               currentProduct.Pnref = parser.nextText();
                              }
                              else if (name == "HostCode "){
                               currentProduct.Host = parser.nextText();
                              }
                              else if (name == "GetCVResultTXT "){
                               currentProduct.Getcv = parser.nextText();
                              }
                              else if (name == "GetCommercialCard"){
                               currentProduct.Getcom = parser.nextText();
                              }
                                else if (name == "ExtData"){
                                    currentProduct.Ext = parser.nextText();
                                }
                      }
                      break;
                  case XmlPullParser.END_TAG:
                      name = parser.getName();
                      Log.d("Resultend", name);
                      if (name.equalsIgnoreCase("Response") &amp;&amp; currentProduct != null){
                       responses.add(currentProduct);
                       //Log.d("tag",responses.toString());
                      }
              }
              eventType = parser.next();
          }
          printProducts(responses);
   }
   private void printProducts(ArrayList&lt;Response&gt; responses)
   {
    String content = "";
    Iterator&lt;Response&gt; it = responses.iterator();
    while(it.hasNext())
    {
     Response currProduct  = it.next();
     content = content + "\n\n\nResult :" +  currProduct.Reslt + "\n";
     content = content + "Respmsg :" +  currProduct.Respmsg + "\n";
     content = content + "Msg :" +  currProduct.Msg + "\n";
     content = content + "Auth :" +  currProduct.Auth + "\n";
     content = content + "Pn :" +  currProduct.Pnref + "\n";
     content = content + "Host :" +  currProduct.Host + "\n";
     content = content + "CV :" +  currProduct.Getcv + "\n";
     content = content + "Com :" +  currProduct.Getcom + "\n";
     content = content + "Ext :" +  currProduct.Ext+ "\n";
    }
    TextView display = (TextView)findViewById(R.id.textView1);
    display.setText(content);
   }
  
  }
  </code></pre>
  
  <p>Please guide me how can we do that. </p>