I am using NSXMLParser
to parse the RSS2 feed of a "Wordpress blog", and pass the contents of certain tags to certain lines or image views within a table view cell. I have read many posts on how to implement NSXMLParser
, however I'm still kinda lost, and would like someone to review my code for errors as well as community input. I feel like there are some parts missing as well. The goal is to parse the title, "pubDate", "img", and "description".
XML:
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:sy="http://purl.org/rss/1.0/modules/syndication/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" version="2.0">
<channel>
<title>Cazenovia Central School District</title>
<atom:link href="http://cazhigh.com/caz/gold/?feed=rss2" rel="self" type="application/rss+xml"/>
<link>http://cazhigh.com/caz/gold</link>
<description>Cazenovia School District</description>
<lastBuildDate>Mon, 24 Mar 2014 02:20:01 +0000</lastBuildDate>
<language>en-US</language>
<sy:updatePeriod>hourly</sy:updatePeriod>
<sy:updateFrequency>1</sy:updateFrequency>
<generator>http://wordpress.org/?v=3.8.3</generator>
<item>
<title>Back-to-school issue of Blue and Gold in the mail</title>
<link>http://cazhigh.com/caz/gold/?p=2896</link>
<comments>http://cazhigh.com/caz/gold/?p=2896#comments</comments>
<pubDate>Thu, 29 Aug 2013 12:35:40 +0000</pubDate>
<dc:creator>
<![CDATA[ Laura Ryan ]]>
</dc:creator>
<category>
<![CDATA[ District Office News ]]>
</category>
<category>
<![CDATA[ News ]]>
</category>
<category>
<![CDATA[ Blue & Gold ]]>
</category>
<guid isPermaLink="false">http://cazhigh.com/caz/gold/?p=2896</guid>
<description>
<![CDATA[
<img width="150" height="150" src="http://cazhigh.com/caz/gold/wp-content/uploads/2013/08/BluGold_BacktoSchool2013_cover-150x150.jpg" class="attachment-thumbnail wp-post-image" alt="Blue and Gold (Back to School 2013)" style="display: block; margin-bottom: 5px; clear:both;" />Be on the lookout for the September 2013 edition of the Blue and Gold newsletter, which should be arriving in the mail soon. Included in the newsletter are district policies and notifications that we urge you to review, become familiar with and keep for your reference throughout the school year. The issue also provides some […]
]]>
</description>
<content:encoded>
<![CDATA[
<img width="150" height="150" src="http://cazhigh.com/caz/gold/wp-content/uploads/2013/08/BluGold_BacktoSchool2013_cover-150x150.jpg" class="attachment-thumbnail wp-post-image" alt="Blue and Gold (Back to School 2013)" style="display: block; margin-bottom: 5px; clear:both;" /><div id="attachment_2898" style="width: 241px" class="wp-caption alignright"><a href="http://cazhigh.com/caz/gold/wp-file-browser-top/blueandgold/Blue%20and%20Gold%20(September%202013)"><img class="size-medium wp-image-2898 " alt="Blue and Gold (Back to School 2013)" src="http://cazhigh.com/caz/gold/wp-content/uploads/2013/08/BluGold_BacktoSchool2013_cover-231x300.jpg" width="231" height="300" /></a><p class="wp-caption-text">Blue and Gold (Back to School 2013)</p></div> <p itemprop="name"><span style="font-size: 13px;">Be on the lookout for the September 2013 edition of the Blue and Gold newsletter, which should be arriving in the mail soon. </span></p> <div itemprop="description articleBody"> <p>Included in the newsletter are district policies and notifications that we urge you to review, become familiar with and keep for your reference throughout the school year.</p> <p>The issue also provides some updates on work undertaken in Cazenovia schools — on everything from curricula to facilities — over the summer break, as well as “welcome back” messages from Superintendent of Schools Robert S. Dubik, Cazenovia High School Principal Eric Schnabl and Assistant Principal Susan Vickers, Cazenovia Middle School Principal Jean Regan and Burton Street Elementary Principal Mary-Ann MacIntosh.</p> <p>If you have questions related to the policies or notifications included in the newsletter, please call the district office at (315) 655-1317.</p> <p>The newsletter is also <span style="color: #000080;"><a title="link to Blue and Gold (September 2013)" href="http://cazhigh.com/caz/gold/wp-file-browser-top/blueandgold/Blue%20and%20Gold%20(September%202013)" target="_blank"><span style="color: #000080;"><b>available online</b></span></a></span>.</p> </div>
]]>
</content:encoded>
<wfw:commentRss>http://cazhigh.com/caz/gold/?feed=rss2&p=2896</wfw:commentRss>
<slash:comments>0</slash:comments>
</item>
viewcontroller.m
:
- (void)viewDidLoad
{
[super viewDidLoad];
NSString *rssaddress =@"http://cazhigh.com/caz/gold/?feed=rss2";
NSURL *url = [NSURL URLWithString:rssaddress];
NSXMLParser *RSSParser = [[NSXMLParser alloc]initWithContentsOfURL:url];
[RSSParser setDelegate:self];
[RSSParser parse];
}
-(void)parser:(NSXMLParser *)RSSParser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict{
if([elementName isEqualToString:@"Title"])
{
//get the attribute
NSString *attributeValue = [attributeDict objectForKey:@"title"];
if([attributeValue isEqualToString:@"pubDate"]) self.dataElementType = 1;
else if([attributeValue isEqualToString:@"description"]) self.dataElementType = 2;
else if([attributeValue isEqualToString:@"img"]) self.dataElementType = 3;
else self.dataElementType = 0;
}
}
-(void)parser:(NSXMLParser *)RSSParser foundCharacters:(NSString *)string {
//Do something with the character inside an element
if([elementName isEqualToString:@"value"])
{
NSString *currentValueString; //to get an NSString from an NSMutableString when working with the timestamp
NSLog(@"%@", currentValueString);
}
}
-(void)parser:(NSXMLParser *)RSSParser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName {
//Do something when you finish an element
NSLog(@"At parserDidEndDocument");
}