need help using NSXMLParser with ASIHTTPRequest

五迷三道 提交于 2020-01-25 11:26:08

问题


I am currently trying to parse the string representation of my xml that I get back from my php script on my server which is passed to the ASIHTTPRequest method *- (void)requestFinished:(ASIHTTPRequest )request

I want to use NSXMLParser to parse this xml, so far I have got everything up and running please see code for how I have done this, however the issue I am having is that it seems to be accessing the *- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString )string{ method multiple times.. when I only want it to access it the one time and return the value. any ideas on why this is happening would be greatly appreciated.

- (void)requestFinished:(ASIHTTPRequest *)request
{
    // Use when fetching text
    NSString *responseString = [request responseString];
    NSData *xData = [responseString dataUsingEncoding:NSUTF8StringEncoding];
    //myCode.text = responseString;
    //NSLog(@" response %@", responseString);

    NSXMLParser *parser = [[NSXMLParser alloc] initWithData:xData];

    [parser setDelegate:self];
    [parser parse];
    [parser release];
}

- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict{
    if ([elementName isEqual:@"code"]) {
        NSLog(@"Found title!");
        titleString = [[NSMutableString alloc] init];
    }
}


- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string{
    [titleString appendString:string];
    NSLog(@"the code string =%@", titleString);

}

//EDITTED:::::

I forgot to add this delegate method which is the last to be called and will output the final result. And the reason I said it was not working was because I wrote something wrong in the method name.... all fixed now and working perfectly.

- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName{
    if ([elementName isEqual:@"code"]) {
        //NSLog(@"ended title: %@", titleString);
        //Pass  code over to animation
        [self parseCodeForAnimation:titleString];

        [titleString release];
        titleString = nil;
    }   

}

回答1:


In general, SAX parsers call the foundCharacters method many times, depending in large part on the contents of the data being parsed. All you can really do is code your app in such a way that it handles this correctly. Typically, you do this by keeping something like an NSMutableData or NSMutableString around and appending to it in each invocation of foundCharacters. When you receive an end tag that signifies that no more data will be sent as part of this element, you dump the mutable contents into a string, store it, and reset the buffer for the next set of data.



来源:https://stackoverflow.com/questions/6576846/need-help-using-nsxmlparser-with-asihttprequest

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!