Mathew
Mathew

Reputation: 575

How to parse XML data from .NET web service soap object in android?

I am new to xml concept. I receive data from .net web service. This web service return dataset as a result. I receive this dataset result using soap object. It returns in XML format. I could not retrieve data from the returned result.

The output for the web service is like this:

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

I want to get each element data. I dont know how to parse it. Please help me out.

This is my code snippet:

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");
   }
}

I tried to parse data using this code. But I could not parse it. Please provide me simple method to parse xml data from soap object response which i got it from .Net webservice dataset.

Thank you in Advance.

Upvotes: 1

Views: 3327

Answers (1)

Mathew
Mathew

Reputation: 575

At last I found solution for this.

        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);

            }
        }

I hope It would be useful for some one. Thank you.

Upvotes: 2

Related Questions