2012-04-30 21 views
1

對不起也許會問一些愚蠢的問題,但我仍然遇到Objective-C語法的問題。 所以,我從我的sudzc.com生成的代碼中得到了這個SOAP響應。它應該包含一個SQL SELECT結果,其中包含veh_idversion作爲列。如何訪問sudzc.com SOAP結果中的數組字段?

我能得到什麼作爲響應對象是一個的NSMutableArray,

NSMutableArray* soapArray = (NSMutableArray*)value; 

所以我穿行:

unsigned count = [soapArray count]; 
while (count--) { 
    id myobj = [soapArray objectAtIndex:count]; 
    NSLog(@"myobj: %@", myobj); 
} 

我得到的打印輸出是一樣的東西:

myobj: { 
item =  { 
    key = version; 
    value = 1; 
}; 

對於SQL結果的每一行。如果這是數組元素的打印輸出,爲什麼只有version列而不是veh_id列?

如何訪問類型爲id的對象myobj上的密鑰值?我必須首先投射嗎?

這是字符串從Zend皁服務器返回的XML:

<?xml version="1.0" encoding="UTF-8"?><SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns1="http://www.[myurl].com/soap" xmlns:ns2="http://xml.apache.org/xml-soap" xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"><SOAP-ENV:Body><ns1:getActiveVehiclesResponse><return SOAP-ENC:arrayType="ns2:Map[23]" xsi:type="SOAP-ENC:Array"><item xsi:type="ns2:Map"><item><key xsi:type="xsd:string">veh_id</key><value xsi:type="xsd:string">1</value></item><item><key xsi:type="xsd:string">version</key><value xsi:type="xsd:string">1</value></item></item><item xsi:type="ns2:Map"><item><key xsi:type="xsd:string">veh_id</key><value xsi:type="xsd:string">3</value></item><item><key xsi:type="xsd:string">version</key><value xsi:type="xsd:string">1</value></item></item><item xsi:type="ns2:Map"><item><key xsi:type="xsd:string">veh_id</key><value xsi:type="xsd:string">4</value></item><item><key xsi:type="xsd:string">version</key><value xsi:type="xsd:string">1</value></item></item></return></ns1:getActiveVehiclesResponse></SOAP-ENV:Body></SOAP-ENV:Envelope> 

回答

0

終於找到了解決方案! 問題在於deserializeAsDictionary函數。 由於我的SOAP XML字符串構成爲具有每個數據庫列項 - 鍵 - 值 - 鍵 - 值等,其添加的關鍵「項」,因此deserializeAsDictionary功能在線路覆蓋下的每一列

[d setObject:v forKey:[child name]] 

已添加的對象。在第一槍,我添加了一個列iterator和現在所說的列「項目1,項目2,......」(進一步優化可能是必要的):

// Deserializes the element in a dictionary. 
+(id)deserializeAsDictionary:(CXMLNode*)element { 
NSLog(@"deserializeAsDictionary = %@, children: %d", element.stringValue, [element childCount]); 

if([element childCount] == 1) { 
    CXMLNode* child = [[element children] objectAtIndex:0]; 
    if([child kind] == CXMLTextKind) { 
     NSLog(@"child %@ added", [child stringValue]); 
     return [[[element children] objectAtIndex:0] stringValue]; 
    } 
} 

NSMutableDictionary* d = [NSMutableDictionary dictionary]; 
NSInteger i = 1; 
NSString *objKey; 
for(CXMLNode* child in [element children]) { 
    id v = [Soap deserialize:child]; 
    if(v == nil) { 
     v = [NSNull null]; 
    } else { 
     if([[child name] isEqualToString:@"(null)"]) { 
      objKey = [NSString stringWithFormat:@"%@",[child stringValue]]; 
     } else if([[child name] isEqualToString:@"key"] || [[child name] isEqualToString:@"value"]) { 
      objKey = [NSString stringWithFormat:@"%@",[child name]]; 
     } else { 
      objKey = [NSString stringWithFormat:@"%@%d",[child name],i++]; 
     } 

    } 

    [d setObject:v forKey:objKey]; 
    NSLog(@"child %@ added", objKey); 
} 
return d; 
} 

結果陣列現在看起來像:

}, 
    { 
    item1 =   { 
     key = "veh_id"; 
     value = 29; 
    }; 
    item2 =   { 
     key = version; 
     value = 1; 
    }; 
} 
1

號。你不需要投它,因爲它顯示所有的數據進賬,我面臨的問題,處理程序方法值(id類型)僅返回第一個元素。

檢查下面的代碼: ...

request = [service myServiceCall:self action:@selector(myHandlerMethod:) param:param1];

...

}

-(void) myHandlerMethod:(id)value{

NSString *xmlString = [[NSString alloc] initWithData:request.receivedData encoding:NSUTF8StringEncoding]; // now if the "value" returned is type as some array of some object,then any arrays don't handle serialization of all the elements of the array it holds. The following code prints just outer tag. (e.g.)

NSLog("%@",xmlString);

}

+0

XML字符串看起來很好 - 你正在談論哪種處理程序方法? – Nachtkrapp

+0

在調用服務時,我們在參數中指定爲選擇器的任何操作,該操作方法的參數是id,如果該類型是自定義數組類型,則該參數僅顯示第一個元素(或無)。看上面的例子。 –

+0

所以我應該用什麼來取得完整的數組返回? – Nachtkrapp

相關問題