2013-04-11 59 views
1

我是xml概念的新手。我從.net Web服務接收數據。此Web服務返回數據集作爲結果。我使用soap對象接收這個數據集結果。它以XML格式返回。我無法從返回的結果中檢索數據。如何從android中的.NET web服務soap對象解析XML數據?

輸出爲Web服務是這樣的:

GETRESULTSResponse{GETRESULTSResult=anyType{Users=anyType{Table1=anyType{StudentID=713; RegisterNumber=2913402; StudentName=KARTHIK M; Gender=Male; CourseID=6; BranchID=27; BatchID=18; RollNumber=10SLEC603; }; }; }; } 

我想每一個元素的數據。我不知道如何解析它。請幫助我。

這是我的代碼片段:

SoapObject request = new SoapObject(WSDL_TARGET_NAMESPACE, METHOD_NAME1); 
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11); 
envelope.dotNet = true; 
PropertyInfo pi = new PropertyInfo(); 
pi.setName("strSQL"); 
pi.setValue(ConstantValues.STUDENT_DETAILS); 
pi.setType(ArrayList.class); 
request.addProperty(pi); 
envelope.setOutputSoapObject(request); 

HttpTransportSE httpTransportSE = new HttpTransportSE(SOAP_ADDRESS); 
SoapObject response = null; 

httpTransportSE.call(SOAP_ACTION1, envelope); 
response = (SoapObject)envelope.bodyIn; 

String xml = response.toString(); 
Document doc = XMLfunctions.XMLfromString(xml); 
int numResults = XMLfunctions.numResults(doc); 

if(totalCount > 0){ 
    NodeList nodes = doc.getElementsByTagName("Table1"); 
    for (int i = 0; i < nodes.getLength(); i++) { 
    Element e = (Element)nodes.item(i); 
    String studentId = XMLfunctions.getValue(e, "StudentID"); 
    String regNo = XMLfunctions.getValue(e, "RegisterNumber"); 
    String stuName = XMLfunctions.getValue(e, "StudentName"); 
    String gender = XMLfunctions.getValue(e, "Gender"); 
    } 
} 

我嘗試使用此代碼來分析數據。但我無法解析它。請給我提供一個簡單的方法來解析SOAP對象響應中的XML數據,我從.Net webservice數據集獲取它。

先謝謝您。

+0

我會a)不返回數據集b)使用[Restfull](http://www.ibm.com/developerworks/webservices/library/ws-restful/)web服務 – I4V 2013-04-11 09:01:03

回答

2

最後我找到了解決方案。

 SoapObject request = new SoapObject(ConstantValues.WSDL_TARGET_NAMESPACE, ConstantValues.METHOD_NAME1); 
     SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11); 
     envelope.dotNet = true; 
     PropertyInfo pi = new PropertyInfo(); 
     pi.setName("strSQL"); 
     pi.setValue(ConstantValues.STUDENT_DETAILS); 
     //pi.setType(ArrayList.class); 
     request.addProperty(pi); 
     envelope.setOutputSoapObject(request); 

     HttpTransportSE httpTransportSE = new HttpTransportSE(ConstantValues.SOAP_ADDRESS); 
     SoapObject response = null; 
     httpTransportSE.debug=true; 
     httpTransportSE.setXmlVersionTag("<?xml version=\"1.0\" encoding=\"utf-8\"?>"); 

     httpTransportSE.call(ConstantValues.SOAP_ACTION1, envelope); 
     response = (SoapObject)envelope.bodyIn; 
     int totalCount = response.getPropertyCount(); 

     String resultString=httpTransportSE.responseDump; 
     Log.d("XML data ",resultString); 

     Document doc = XMLfunctions.XMLfromString(resultString); 

     //int numResults = XMLfunctions.numResults(doc); 

     System.out.println(totalCount); 
     if(totalCount > 0){ 
      NodeList nodes = doc.getElementsByTagName("Table1"); 
      for (int i = 0; i < nodes.getLength(); i++) { 
       studentData = new StudentDetailsData(); 
       Element e = (Element)nodes.item(i); 

       studentData.setStudentId(Integer.parseInt(XMLfunctions.getValue(e, "StudentID"))); 
       studentData.setRegisterNo(XMLfunctions.getValue(e, "RegisterNumber")); 
       studentData.setStudentName(XMLfunctions.getValue(e, "StudentName")); 
       studentData.setGender(XMLfunctions.getValue(e, "Gender")); 
       studentData.setCourseId(Integer.parseInt(XMLfunctions.getValue(e, "CourseID"))); 
       studentData.setBranchId(Integer.parseInt(XMLfunctions.getValue(e, "BranchID"))); 
       studentData.setBatchId(Integer.parseInt(XMLfunctions.getValue(e, "BatchID"))); 
       studentData.setRollNo(XMLfunctions.getValue(e, "RollNumber")); 
       studentData.setSection(XMLfunctions.getValue(e, "Section")); 

       result.add(studentData); 

      } 
     } 

我希望這對某些人會有用。謝謝。