Friday, 9 August 2013

XML Parsing in Android

XML Parsing in Android

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.
<?xml version="1.0" encoding="utf-8"?>
<Response xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns="http://tempuri.org/">
<Result xmlns="GlobalPayments">0</Result>
<RespMSG xmlns="GlobalPayments">Approved</RespMSG>
<Message xmlns="GlobalPayments">AP</Message>
<AuthCode xmlns="GlobalPayments">01245F</AuthCode>
<PNRef xmlns="GlobalPayments">170015964</PNRef>
<HostCode xmlns="GlobalPayments">002</HostCode>
<GetCVResultTXT xmlns="GlobalPayments">Service Not
Requested</GetCVResultTXT>
<GetCommercialCard xmlns="GlobalPayments">True</GetCommercialCard>
<ExtData
xmlns="GlobalPayments">CardType=Visa,BatchNum=0202&lt;BatchNum&gt;0202&lt;/BatchNum&gt;&lt;ReceiptData&gt;&lt;MID&gt;87228799&lt;/MID&gt;&lt;Trans_Id&gt;283220167525&lt;/Trans_Id&gt;&lt;Val_Code&gt;52ND&lt;/Val_Code&gt;&lt;/ReceiptData&gt;</ExtData>
</Response>
I am using the below code and the Issue is the XmlPullParser.START_TAG:
gives the whole tags available into the xml not required one tag.
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<Response> 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<Response>();
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") &&
currentProduct != null){
responses.add(currentProduct);
//Log.d("tag",responses.toString());
}
}
eventType = parser.next();
}
printProducts(responses);
}
private void printProducts(ArrayList<Response> responses)
{
String content = "";
Iterator<Response> 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);
}
}
Please guide me how can we do that.

No comments:

Post a Comment