NSXMLParser委托方法无法正常工作

时间:2013-09-23 15:07:14

标签: iphone ios nsxmlparserdelegate

首次使用NSXMLParser委托方法解析XML数据时,它可以正常工作。 但是当我使用不同的XML数据执行相同操作时,它会向我显示先前解析的数据而不是新的XML数据 didStartElement获取以前XML数据的elementName,不会更改新元素名称。

- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
    NSString *citystate= [[NSString alloc]initWithData:cityStateData encoding:NSUTF8StringEncoding];
    //NSLog(citystate);
    NSData *citiesStates = [citystate dataUsingEncoding:NSUTF8StringEncoding];

    xmlParser = [[NSXMLParser alloc] initWithData:citiesStates];

    // Don't forget to set the delegate!
    xmlParser.delegate = self;

    // Run the parser
    //BOOL parsingResult = [xmlParser parse];
    [xmlParser parse];
}

-(void) parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName: (NSString *)qName attributes:(NSDictionary *)attributeDict
{
    allCityState = [[NSMutableString alloc] init];
}

- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string
{
    [allCityState appendString:string];
}

- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName
{
    if([elementName isEqual: @"CityComboResult"])
    {
        NSString* cities = [allCityState copy];
        NSLog(cities);
        allCityState = nil;
    }
    else if([elementName isEqual: @"StateComboResult"])
    {
        NSString* states = [allCityState copy];
        NSLog(states);
        allCityState = nil;
    }
}

0 个答案:

没有答案