2010-08-04 92 views
3

我的另一個問題OBJ-C: 我試圖解析這個XML:與touchXML解析SOAP響應 - 問題

這裏的情況:

<?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> 
     <GetMyDashboardResponse xmlns="http://tempuri.org/"> 
      <GetMyDashboardResult> 
       <MyDashboard> 
        <Profile>Speed Seeker</Profile> 
        <UserID>6</UserID> 
        <VenueName>Prasowa</VenueName> 
        <FriendsCount>10</FriendsCount> 
        <FollowingCount>11</FollowingCount> 
        <FollowersCount>12</FollowersCount> 
        <StatusMessage>aaa</StatusMessage> 
        <StatusDate>2010-08-04T11:38:09.733</StatusDate> 
        <CheckedInTimeStamp>2010-07-30T13:48:24</CheckedInTimeStamp> 
       </MyDashboard> 
      </GetMyDashboardResult> 
     </GetMyDashboardResponse> 
    </soap:Body> 
</soap:Envelope> 

和這裏的如何我「M試圖完成它:

NSMutableArray *res = [[NSMutableArray alloc] init]; 

CXMLDocument *doc = [[[CXMLDocument alloc] initWithData:XMLData options:0 error:nil] autorelease]; 

NSArray *nodes = NULL; 
nodes = [doc nodesForXPath:@"//MyDashboard" error:nil]; 

for (CXMLElement *node in nodes) 
{ 
     NSMutableDictionary *item = [[NSMutableDictionary alloc] init]; 
     int counter; 
     for(counter = 0; counter < [node childCount]; counter++) 
     { 
      [item setObject:[[node childAtIndex:counter] stringValue] forKey:[[node childAtIndex:counter] name]]; 
     } 
     [item setObject:[[node elementsForName:@"Profile"] stringValue] forKey:@"profileName"]; // <------ this magical arrow is pointing to the area of interest 
    NSLog(@"petla"); 
     [res addObject:item]; 
     [item release]; 
} 

NSLog(@"%@", res); 
[res release]; 

`

感謝您的幫助。

回答

1

[node elementsForName]將返回NSArray,所以你絕對不能使用stringValue。創建臨時NSArray,設置elementsForName,檢查它的對象數是否大於0,取objectAtIndex:0,你很好走!