Search code examples
androidblackberryksoap2

How to parse complex SOAP Response in KSOAP2


I am getting SOAP response which is a bit complex. The response is like this:

    <?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
  <soap:Body>
    <getDetailResponse xmlns="http://saiserviceapp.com/">
      <getDetailResult>
        <UserDetail>
          <UserID>int</UserID>
          <VehicleID>int</VehicleID>
          <UserName>string</UserName>
          <VehicleNo>string</VehicleNo>
          <ModelID>int</ModelID>
          <VariantID>int</VariantID>
          <Color>string</Color>
          <DOP>dateTime</DOP>
          <InsCompany>string</InsCompany>
          <InsExpire>dateTime</InsExpire>
          <ContactNo>long</ContactNo>
          <DInsExpire>dateTime</DInsExpire>
        </UserDetail>
        <UserDetail>
          <UserID>int</UserID>
          <VehicleID>int</VehicleID>
          <UserName>string</UserName>
          <VehicleNo>string</VehicleNo>
          <ModelID>int</ModelID>
          <VariantID>int</VariantID>
          <Color>string</Color>
          <DOP>dateTime</DOP>
          <InsCompany>string</InsCompany>
          <InsExpire>dateTime</InsExpire>
          <ContactNo>long</ContactNo>
          <DInsExpire>dateTime</DInsExpire>
        </UserDetail>
      </getDetailResult>
    </getDetailResponse>
  </soap:Body>
</soap:Envelope>

Now to parse it i have written this code:

 resultRequestSOAP = (SoapObject)envelope.bodyIn;
    SoapObject root = (SoapObject) resultRequestSOAP.getProperty(mStrProperty);
    SoapObject childObj[] = new SoapObject[root.getPropertyCount()];
    for(int i = 0; i < root.getPropertyCount(); i++)
    {
        childObj[i] = (SoapObject) root.getProperty("UserDetail");
        vector.addElement(childObj[i].getProperty(3));
    }

Here i am getting the root value after converting to string like below:

    anyType
{
 UserDetail=
      anyType{
          UserID=10884; 
          VehicleID=507; 
          UserName=ffasdd; 
          VehicleNo=GJGJGJG; 
          ModelID=0; 
          VariantID=0; 
          DOP=0001-01-01T00:00:00; 
          InsExpire=null; 
          ContactNo=8888555522; 
          DInsExpire=0001-01-01T00:00:00; 
          }; 
  UserDetail=
        anyType{
            UserID=10884; 
            VehicleID=508; 
            UserName=ffasdd; 
            VehicleNo=HGHGGHJ; 
            ModelID=0; 
            VariantID=0; 
            DOP=0001-01-01T00:00:00; 
            InsExpire=null; 
            ContactNo=8888555522; 
            DInsExpire=0001-01-01T00:00:00; 
            }; 
}

Now from this complex response i want the vehicle numbers and want to store them in vector. Currently with my above code, i am getting the first set of response multiple times. Means same vehicleNo is added multiple time in the vector.

How can i resolve this Issue.


Solution

  • I am able to resolve the issue in the below way:

    for(int i = 0; i < root.getPropertyCount(); i++)
                {
                    Object property = root.getProperty(i);
                     if (property instanceof SoapObject)
                       {
                         SoapObject category_list = (SoapObject) property;
                         String strVehNo = category_list.getProperty("VehicleNo").toString();
                         String strVehId = category_list.getProperty("VehicleID").toString();
                         vector.addElement(strVehNo);
                         vector.addElement(strVehId);
                       }
                }
            }