I wrote some code, most of which was taken from textbooks, etc., but not sure if it is 100%. The code works, but I think it is a little long and has memory leaks.
So, three questions:
- Can I clean it? Easier to understand?
- How can I fix 'if (indexPath.section == 1) theRow + = 6;' bit for dynamic?
- Memory leaks, how can I sort if they need to be sorted? (Newbie ...)
Any feedback / pointers / help is more than welcome ...
#import "InfoViewController.h" @implementation InfoViewController - (void)parseXMLFileAtURL:(NSString *)URL { sections = [[NSMutableArray alloc] init]; secItems = [[NSMutableArray alloc] init]; //you must then convert the path to a proper NSURL or it won't work NSURL *xmlURL = [NSURL URLWithString:URL]; rssParser = [[NSXMLParser alloc] initWithContentsOfURL:xmlURL]; // Set self as the delegate of the parser so that it will receive the parser delegate methods callbacks. [rssParser setDelegate:self]; // Depending on the XML document you're parsing, you may want to enable these features of NSXMLParser. [rssParser setShouldProcessNamespaces:NO]; [rssParser setShouldReportNamespacePrefixes:NO]; [rssParser setShouldResolveExternalEntities:NO]; [rssParser parse]; } - (void)parserDidStartDocument:(NSXMLParser *)parser { [activityIndicator startAnimating]; [activityIndicator addSubview]; } - (void)parser:(NSXMLParser *)parser parseErrorOccurred:(NSError *)parseError { NSString * errorString = [NSString stringWithFormat:@"Unable to download story feed from web site (Error code %i )", [parseError code]]; NSLog(@"error parsing XML: %@", errorString); UIAlertView * errorAlert = [[UIAlertView alloc] initWithTitle:@"Error loading content" message:errorString delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil]; [errorAlert show]; } - (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict{ currentElement = [elementName copy]; if ([elementName isEqualToString:@"title"]) { // clear out our story item caches... currentTitle = [[NSMutableString alloc] init]; } if ([elementName isEqualToString:@"section"]) { //item = [[NSMutableDictionary alloc] init]; item = [[NSMutableDictionary alloc] init]; currentSection = [[NSMutableString alloc] init]; } } - (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName{ if ([elementName isEqualToString:@"section"]) { // save values to an item, then store that item into the array... [item setObject:currentSection forKey:@"name"]; [item setObject:[NSNumber numberWithInt:itemsCount] forKey:@"itemsCount"]; [sections addObject:[item copy]]; itemsCount = 0; } if ([elementName isEqualToString:@"title"]) { // save values to an item, then store that item into the array... [item setObject:currentTitle forKey:@"title"]; itemsCount = itemsCount + 1; [secItems addObject:[item copy]]; } } - (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string{ // save the characters for the current item... if ([currentElement isEqualToString:@"name"]) { [currentSection appendString:string]; } else if ([currentElement isEqualToString:@"title"]) { [currentTitle appendString:string]; } else if ([currentElement isEqualToString:@"description"]) { [currentSummary appendString:string]; } else if ([currentElement isEqualToString:@"pubDate"]) { [currentDate appendString:string]; } } - (void)parserDidEndDocument:(NSXMLParser *)parser { [activityIndicator stopAnimating]; [activityIndicator removeFromSuperview]; [dataTable reloadData]; } - (void)viewDidAppear:(BOOL)animated { [super viewDidAppear:animated]; if ([sections count] == 0) { NSString * path = @"http://www.website.com/_dev/info.xml"; [self parseXMLFileAtURL:path]; } }
The XML feed I am trying to break up into sections and elements:
<?xml version="1.0" encoding="ISO-8859-1"?> <data> <section> <name>Section 1</name> <item> <title>Title 1</title> <pubDate>1 Sept 2010</pubDate> <detail><![CDATA[Detail text]]></detail> </item> <item> <title>Title 2</title> <pubDate>1 Sept 2010</pubDate> <detail><![CDATA[<Detail text]]></detail> </item> <item> <title>Title 3</title> <pubDate>1 Sept 2010</pubDate> <detail><![CDATA[Detail text]]></detail> </item> <item> <title>Title 4</title> <pubDate>1 Sept 2010</pubDate> <detail><![CDATA[Detail text]]></detail> </item> <item> <title>Title 5</title> <pubDate>1 Sept 2010</pubDate> <detail><![CDATA[Detail text]]></detail> </item> <item> <title>Title 6</title> <pubDate>1 Sept 2010</pubDate> <detail><![CDATA[Detail text]]></detail> </item> </section> <section> <name>Section 2</name> <item> <title>Title 1</title> <pubDate>1 Sept 2010</pubDate> <detail><![CDATA[Detail text]]></detail> </item> <item> <title>Title 2</title> <pubDate>1 Sept 2010</pubDate> <detail><![CDATA[Detail text]]></detail> </item> </section> </data>
jimbo
source share