2013-07-04 28 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]; 
}