解析ksoap响应

时间:2012-02-29 13:08:54

标签: java soap ksoap

作出回应

transport.call(SOAP_ACTION, envelope, headerList);

responseDump 的调试模式中我有像这样的xml

<?xml version="1.0" encoding="utf-8"?><soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"><soap:Body><GetListItemsResponse xmlns="http://schemas.microsoft.com/sharepoint/soap/"><GetListItemsResult><listitems xmlns:s='uuid:BDC6E3F0-6DA3-11d1-A2A3-00AA00C14882'
     xmlns:dt='uuid:C2F41010-65B3-11d1-A29F-00AA00C14882'
     xmlns:rs='urn:schemas-microsoft-com:rowset'
     xmlns:z='#RowsetSchema'>
<rs:data ItemCount="5">
   <z:row ows_Attachments='0' ows_LinkTitle='testtask1' ows_Status='Not Started' ows_Priority='(2) Normal' ows_Predecessors='' ows__ModerationStatus='0' ows__Level='1' ows_Title='testtask1' ows_ID='1' ows_UniqueId='1;#{A6E41E02-46FD-4363-A4A3-440AF902B280}' ows_owshiddenversion='1' ows_FSObjType='1;#0' ows_Created_x0020_Date='1;#2012-02-28 09:55:49' ows_Created='2012-02-28 09:55:49' ows_FileLeafRef='1;#1_.000' ows_PermMask='0x7fffffffffffffff' ows_Modified='2012-02-28 09:55:49' ows_FileRef='1;#Lists/Tasks/1_.000' />
   <z:row ows_Attachments='0' ows_LinkTitle='testtask2' ows_Status='Not Started' ows_Priority='(1) High' ows_Predecessors='' ows__ModerationStatus='0' ows__Level='1' ows_Title='testtask2' ows_ID='2' ows_UniqueId='2;#{A320D923-8BC8-4341-B40A-4D39C1FB01DB}' ows_owshiddenversion='1' ows_FSObjType='2;#0' ows_Created_x0020_Date='2;#2012-02-28 09:55:58' ows_Created='2012-02-28 09:55:58' ows_FileLeafRef='2;#2_.000' ows_PermMask='0x7fffffffffffffff' ows_Modified='2012-02-28 09:55:58' ows_FileRef='2;#Lists/Tasks/2_.000' />
   <z:row ows_Attachments='0' ows_LinkTitle='testtask3' ows_Status='In Progress' ows_Priority='(2) Normal' ows_Predecessors='' ows__ModerationStatus='0' ows__Level='1' ows_Title='testtask3' ows_ID='3' ows_UniqueId='3;#{84071EBA-F38D-4EA9-A912-F8DD0088BD38}' ows_owshiddenversion='1' ows_FSObjType='3;#0' ows_Created_x0020_Date='3;#2012-02-28 09:56:07' ows_Created='2012-02-28 09:56:07' ows_FileLeafRef='3;#3_.000' ows_PermMask='0x7fffffffffffffff' ows_Modified='2012-02-28 09:56:07' ows_FileRef='3;#Lists/Tasks/3_.000' />
   <z:row ows_Attachments='0' ows_LinkTitle='testtask4' ows_Status='Waiting on someone else' ows_Priority='(3) Low' ows_Predecessors='' ows__ModerationStatus='0' ows__Level='1' ows_Title='testtask4' ows_ID='4' ows_UniqueId='4;#{AD6ED317-3F07-4998-89D2-38D8B6F0756E}' ows_owshiddenversion='1' ows_FSObjType='4;#0' ows_Created_x0020_Date='4;#2012-02-28 09:56:16' ows_Created='2012-02-28 09:56:16' ows_FileLeafRef='4;#4_.000' ows_PermMask='0x7fffffffffffffff' ows_Modified='2012-02-28 09:56:16' ows_FileRef='4;#Lists/Tasks/4_.000' />
   <z:row ows_Attachments='0' ows_LinkTitle='testtask5' ows_Status='Not Started' ows_Priority='(2) Normal' ows_Predecessors='' ows__ModerationStatus='0' ows__Level='1' ows_Title='testtask5' ows_ID='5' ows_UniqueId='5;#{50BC423F-E6A5-4719-B734-E528A08AE83B}' ows_owshiddenversion='1' ows_FSObjType='5;#0' ows_Created_x0020_Date='5;#2012-02-28 19:22:52' ows_Created='2012-02-28 19:22:52' ows_FileLeafRef='5;#5_.000' ows_PermMask='0x7fffffffffffffff' ows_Modified='2012-02-28 19:22:52' ows_FileRef='5;#Lists/Tasks/5_.000' />
</rs:data>
</listitems></GetListItemsResult></GetListItemsResponse></soap:Body></soap:Envelope>

但方法 envelope.getResponse()返回

下面的文字
anyType{listitems=anyType{data=anyType{row=anyType{}; row=anyType{}; row=anyType{}; row=anyType{}; row=anyType{}; }; }; }

UPD1 我读了这个acticle,但这不是我的回答的解决方案。

好的,我正在尝试解析下面的答案

 transport.call(SOAP_ACTION2, envelope, headerList); // 1st step - call soap query
 SoapObject response = (SoapObject) envelope.getResponse();
 SoapObject soapObject = (SoapObject) envelope.getResponse();
 String title= null;
    for (int i = 0; i < soapObject.getPropertyCount(); i++) { // soapObject.getPropertyCount()  is only one (!!!!)
        SoapObject resource = (SoapObject) response.getProperty(i); // in resource only this - anyType{data=anyType{row=anyType{}; row=anyType{}; row=anyType{}; row=anyType{}; row=anyType{}; }; }
        title = resource.getAttribute("ows_Title").toString();
....

1 个答案:

答案 0 :(得分:3)

你试过这样的事吗:

SoapObject soapObject = envelope.getResponse();
for (int i = 0; i < soapObject.getPropertyCount(); i++) {
        SoapObject resource = (SoapObject) response.getProperty(i);
        String uri = resource.getAttribute("uri").toString();
        String mimeType = resource.getAttribute("mimeType").toString();
        String size = resource.getAttribute("size").toString();
        String localURI = resource.getAttribute("localURI").toString();
        //do stuff
}    

注意:这是我用来从资源列表中获取属性的示例

此示例循环遍历响应SOAP对象的标记,并获取每个标记的3个已知属性,请参阅下面的示例响应:

<resources xmlns="http://www.example.com/">
    <resource mimeType="text/xml" size="123456" uri="https://example.com/content/get.php?a123891" localURI="package.opf"/>
    <resource mimeType="application/x-dtbncx+xml" size="123456"  localURI="nav.ncx"/>
</resources>