如何在KSOAP2中解析复杂的SOAP响应

时间:2013-05-27 20:36:20

标签: android blackberry ksoap2

我收到的SOAP响应有点复杂。答案是这样的:

    <?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>

现在解析它我写了这段代码:

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

这里我在转换为字符串之后得到了根值,如下所示:

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

现在从这个复杂的响应中我想要车辆编号,并希望将它们存储在矢量中。 目前使用我的上述代码,我多次获得第一组响应。意味着在载体中多次添加相同的载体。

我如何解决此问题。

3 个答案:

答案 0 :(得分:2)

我认为此代码会对您有所帮助。

int count = root.getPropertyCount();

for(int i=0; i< count; i++)
{
    SoapObject response = (SoapObject) envelope.getResponse();                          
    SoapObject root = (SoapObject) response.getProperty("UserDetail");
    String userID = (root.getPropertyAsString("UserID"));  
    String userName = (root.getPropertyAsString("UserName"));
}

答案 1 :(得分:0)

(SoapObject) root.getProperty("UserDetail");始终与root

的位置相同

答案 2 :(得分:0)

我可以通过以下方式解决问题:

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