I’m using a RSS Reader and works fine when I tap the UITableViewCell to load the <link> either in a UIWebView or to open Safari on that link.
But I really want to learn how to load the Topic content into the application instead showing the entire site or jump to Safari
In the RSS feed per each <item> there is a <body> tag (and a <Description> that contains the same but encoded) that contains the topic content, like the image below shows:
alt text http://www.balexandre.com/temp/2010-04-13_0953.png
So, instead of catching the <link> I’m assigning the <body>. Problem is that it does not work correctly 🙁
for this example I only get the content until the first <br> nothing more.
- I’m using a
NSStringas I would use in C#, should I use any other object, is there a better object to use on such data? - Should I use an
UITextViewto load this information (as it has scroll already) or I should use aUIWebViewinstead?
Thank you.
added
- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict{
//NSLog(@"found this element: %@", elementName);
currentElement = [elementName copy];
if ([elementName isEqualToString:@"item"]) {
// clear out our story item caches...
item = [[NSMutableDictionary alloc] init];
currentTitle = [[NSMutableString alloc] init];
currentDate = [[NSMutableString alloc] init];
currentSummary = [[NSMutableString alloc] init];
currentLink = [[NSMutableString alloc] init];
currentBody = [NSMutableString new]; // added by me
currentCreator = [NSMutableString new]; // added by me
}
}
- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName{
//NSLog(@"ended element: %@", elementName);
if ([elementName isEqualToString:@"item"]) {
// save values to an item, then store that item into the array...
[item setObject:currentTitle forKey:@"title"];
[item setObject:currentLink forKey:@"link"];
[item setObject:currentSummary forKey:@"summary"];
[item setObject:currentDate forKey:@"date"];
[item setObject:currentBody forKey:@"body"]; // <----
[item setObject:currentCreator forKey:@"dc:creator"];
[stories addObject:[item copy]];
NSLog(@"adding story: %@", currentTitle);
}
}
- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string{
//NSLog(@"found characters: %@", string);
// save the characters for the current item...
if ([currentElement isEqualToString:@"title"]) {
[currentTitle appendString:string];
} else if ([currentElement isEqualToString:@"link"]) {
[currentLink appendString:string];
} else if ([currentElement isEqualToString:@"summary"]) {
[currentSummary appendString:string];
} else if ([currentElement isEqualToString:@"pubDate"]) {
[currentDate appendString:string];
} else if ([currentElement isEqualToString:@"body"]) {
[currentBody appendString:string]; // <----
} else if ([currentElement isEqualToString:@"dc:creator"]) {
[currentCreator appendString:string];
}
}
and to pass to my WebBrowser View I have:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
// Navigation logic
int storyIndex = [indexPath indexAtPosition: [indexPath length] - 1];
NSString * storyContent = [[stories objectAtIndex: storyIndex] objectForKey: @"body"];
// load web view
[self showWebPage:storyContent]; // <-- Here (storyContent variable) I only have the content until the first <br> :-(
}
Take a look at the SeismicXML sample project from Apple. It gives you a general idea of how to use
NSXMLParser, and is also written to parse the XML data in its own thread to allow the user to still interact with the UI. If you are looking for a nested tag (i.e.<body>within<item>), you will want a flag to tell if you are currently within the<item>tag or not. Otherwise you will parse all<body>tags.To address your second question, it depends on how you want to present this information to the user. If you want the text styled, you will need to use a UIWebView. Otherwise you can strip out what you need and have that spread out through a UITableView, or a custom view you create in Interface Builder.
Edit: After seeing your last comment, you need to create a flag (i.e.
insideBody) and check for that inside offoundCharacters:You will probably have to do the same in
didStartElement:anddidEndElement:, as getting data fromfoundCharacters:will only give you the content of tags and not the tags themselves.