使用iOS中的NSXMLParsing在字典中创建数组内容

时间:2013-07-04 09:18:01

标签: iphone ios objective-c parsing nsxmlparser

下午溢出,

在我的代码中,我正在尝试解析NSData格式的SOAP响应。有了这些数据,使用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];
        }
    }
}

提前谢谢你。 此致

3 个答案:

答案 0 :(得分:0)

而不是:

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

尝试:

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

因此,您不必删除字典中的所有项目,而是为下一组内容创建新字典。

答案 1 :(得分: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];
}

答案 2 :(得分: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];
}