2013-07-04 27 views
1

下午overflowers,創建使用NSXMLParsing iOS中

在我的代碼,我試圖解析和字典SOAP響應就是在NSData的格式的數組的內容。有了這些數據,使用NSXMLParse,我試圖創建一個字典數組。問題是,無論何時將新的字典添加到數組中,它都會使用當前添加的舊字典對象內容替換舊的字典對象內容。例如,在解析結束時,我有7個詞典在我的數組中具有相同的內容。這裏是顯示我所做的工作的代碼;

- (void)connectionDidFinishLoading:(NSURLConnection *)connection { 
    NSLog(@"Received the SOAP data."); 

    if (!itemArray) { 
     itemArray = [[NSMutableArray alloc] init]; 
    } 
    else { 
     itemArray = nil; 
     itemArray = [[NSMutableArray alloc] init]; 
    } 

    if (!itemDictionary) { 
     itemDictionary = [[NSMutableDictionary alloc] init]; 
    } 
    else { 
     itemDictionary = nil; 
     itemDictionary = [[NSMutableDictionary alloc] init]; 
    } 

    parser = [[NSXMLParser alloc] initWithData:webData]; 
    [parser setDelegate:self]; 
    [parser parse]; 
} 

- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qualifiedName attributes:(NSDictionary *)attributeDict { 
    NSLog(@"Started Element %@", elementName); 
    element = [NSMutableString string]; 
} 

- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string { 
    if(element == nil) { 
     element = [[NSMutableString alloc] init]; 
    } 

    [element appendString:string]; 
} 

- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName { 
    NSLog(@"Found an element named: %@ with a value of: %@", elementName, element); 

    if (![elementName isEqualToString:@"item"]) { 
     [itemDictionary setValue:element forKey:elementName]; 
    } 
    else if ([elementName isEqualToString:@"item"]) { 
     if (itemDictionary) { 
      [itemArray addObject:itemDictionary]; 

      [itemDictionary removeAllObjects]; 
     } 
    } 
} 

在此先感謝您。 問候

回答

0

相反的:

[itemArray addObject:itemDictionary]; 
[itemDictionary removeAllObjects]; 

嘗試:的

[itemArray addObject:itemDictionary]; 
itemDictionary = [[NSMutableDictionary alloc] init]; 

所以不是從你的字典中刪除所有的項目創建下一組的內容的新字典。

+0

這幫了很多,謝謝 – Engnyl

0

我想你應該是因爲每個itemDictionary都是需要存儲在itemArray中的單個對象。因此,一旦啓動元素,您就必須創建一個dictionay實例。嘗試像這樣

- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qualifiedName attributes:(NSDictionary *)attributeDict { 
    NSLog(@"Started Element %@", elementName); 
    itemDictionary = [[NSMutableDictionary alloc] init]; 
    element = [NSMutableString string]; 
} 
0

當你調用removeAllObjects時,它只是使用你的數組添加的同一個字典實例。當你修改你的字典時,它會修改你的數組包含的同一個字典(因爲兩者實際上是相同的),所以不要調用removeAllObjects,而是在每次新元素啓動時創建新的字典。

- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qualifiedName attributes:(NSDictionary *)attributeDict { 
    NSLog(@"Started Element %@", elementName); 
    itemDictionary = [[NSMutableDictionary alloc] init]; 
    element = [NSMutableString string]; 
}