NSXMLParser not parsing attributes. No NSXMLParser Error.
Posted
by Iris
on Stack Overflow
See other posts from Stack Overflow
or by Iris
Published on 2010-05-12T20:00:28Z
Indexed on
2010/05/12
20:04 UTC
Read the original article
Hit count: 428
iphone
|nsxmlparser
I am trying to parse the XML located at this URL: http://www.iglooseat.com/gis_iphone_xml.php?zip=06488
// 1) This method gets invoked when the user presses a button on the Iphone to retrieve the xml data
(IBAction)getLocations:(id)sender { NSString *msg=nil;
NSString *urlString= [[NSString alloc] initWithFormat:@"http://www.iglooseat.com/gis_iphone_xml.php?zip=%@",zipField.text];
// send the URL NSError *error; [siteList updateLocationsFromURL:urlString parseError:&error]; WPSite *w = [siteList siteAtIndex:0];
// alert user what's in the zipField msg = [[NSString alloc] initWithFormat: @"url to send: %@\n site name: %@" , urlString , w.name];
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Debug" message:msg delegate:self cancelButtonTitle:@"Ok" otherButtonTitles:nil];
[urlString release];
[alert show]; [alert release]; [msg release]; }
// 2) This function takes the URL and performs XML parsing on the data at that URL. (void)updateLocationsFromURL:(NSString )urlString parseError:(NSError *)error{
/* NSURL class that inherits from NSObject class that provides a way to manipulate URLs and the resources they reference. */
NSURL *url = [[NSURL alloc] initWithString:urlString];
/* initWithContentsOfURL: initializes a newly allocated data object initialized with the data from the location specified by a URL. */
NSXMLParser *parser = [[NSXMLParser alloc] initWithContentsOfURL:url];
// init bool to NO errorConnecting = NO;
// release from mem the NSURfile://localhost/Users/icheung/Desktop/WPMap/Classes/WPSite.mL [url release];
// set parser delgate to self [parser setDelegate:self];
// don't process namespace [parser setShouldProcessNamespaces:YES];
// or namespace prefixes [parser setShouldReportNamespacePrefixes:NO];
/* don't care 'bout external (ex. don't perform I/O op's to load external DTD's (Document Type Definitions)) */
[parser setShouldResolveExternalEntities:NO];
// start the event-driven parsing operation [parser parse];
// get NSError object if an error occured during parsing NSError *parseError = [parser parserError]; if(parseError && error) { *error = parseError; NSLog(@"Error code:%d %@", parseError.code, parseError.domain); errorConnecting = YES; }
// relase from mem the parser [parser release]; }
// 3) In the parser:didStartElement:namespaceURI:qualifiedName:attributes: I attempt to extract the 'state' attribute from a 'marker' element in my xml. When I use the gdb debugger to inspect the contents of attributeDict it is empty and I'm not sure why.
(void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict {
if(self.parserContentOfCurrentProperty == nil) { NSMutableString *tmp = [[NSMutableString alloc] init]; self.parserContentOfCurrentProperty = tmp; [tmp release]; }
[self.parserContentOfCurrentProperty setString:@""];
self.parserNameOfCurrentProperty = elementName; if ([elementName isEqualToString:@"markers"]) { WPSite *tmp = [WPSite alloc]; self.parserCurrentSite = tmp;
// retrive value for attribute NSString *stateAttribute = [attributeDict valueForKey:@"state"]; if (stateAttribute) { [self.parserCurrentSite setState:stateAttribute]; }
// add this site to the site list [siteList addObject:self.parserCurrentSite]; return; } }
© Stack Overflow or respective owner