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;
}
}