经过一些操作后,我得到了来自服务器的 bewlow xml 响应。现在我需要从这些标签中获取数据。请指导我如何获得它。
<?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<BatchNum>0202</BatchNum><ReceiptData><MID>87228799</MID><Trans_Id>283220167525</Trans_Id><Val_Code>52ND</Val_Code></ReceiptData></ExtData>
</Response>
我正在使用下面的代码,问题是 XmlPullParser.START_TAG:提供了 xml 中可用的整个标签,而不需要一个标签。
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);
}
}
请指导我如何做到这一点。