views:

79

answers:

2

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

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 NSString as 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 UITextView to load this information (as it has scroll already) or I should use a UIWebView instead?

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> :-(
}
A: 

Use NSXMLParser to detect the <body> and </body> tags, and just grab everything between them into a string.

Now that I can see your code, I can see your bug. When you encounter the <br /> tag you change the currentElement to @"br", which means that you stop adding characters to currentBody.

Graham Lee
I'm doing that already - the string does not contain everything, just until the first <br> witch is weird
balexandre
A: 

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 of foundCharacters:

- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string{
    //NSLog(@"found characters: %@", string);
    // save the characters for the current item...
    if(insideBody == YES) {
        [currentBody appendString:string];
    } else 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:@"dc:creator"]) {
        [currentCreator appendString:string];
    }
}

You will probably have to do the same in didStartElement: and didEndElement:, as getting data from foundCharacters: will only give you the content of tags and not the tags themselves.

mgriepentrog
I'm using the NSXMLParser already to get the the RSS, this is just for the `<body>` tag
balexandre
Are you using an NSMutableString to construct the string inside the `<body>` tag? If memory serves, it should continue until the close of the tag (unless it encounters a tag inside which you are parsing for elsewhere).
mgriepentrog
added my code ... I do use a NSMutableString :-/
balexandre
using the `foundCharacters` method, I can see that it does not get the correct content as it gets elements for `<p>` `<br>` etc, and it should treat this as data and not more elements inside the `<body>`
balexandre