开发者

HTML Parsing in iphone sdk

I am using hpple from git for parsing HTML. It working fine. But when I get the parsed NSString I found that in this string double inverted开发者_运维技巧 comma (")and single (') are replaced by some other symbol like ,Äô. How can I get the correct string? I have tried to replace these characters but its not working.


Check out this link it solved my problem

https://github.com/mwaterfall/MWFeedParser

Here the steps to add the code

Add all the classes,except detailtableviewcontroller and Rootviewcontroller from the code you downloaded from the link.  Then add #import "MWFeedParser.h" in your.h file where you are parsing .Then add // Parsing
MWFeedParser *feedParser;
NSMutableArray *parsedItems;

// Displaying
NSArray *itemsToDisplay;
NSDateFormatter *formatter;and /***mwfeed (*/
@property (nonatomic, retain) NSArray *itemsToDisplay;    
/*------------*/  
Then in .m add the codeformatter = [[NSDateFormatter alloc] init];
[formatter setDateStyle:NSDateFormatterShortStyle];
[formatter setTimeStyle:NSDateFormatterShortStyle];
parsedItems = [[NSMutableArray alloc] init];
self.itemsToDisplay = [NSArray array];
// Parse
NSURL *feedURL = [NSURL URLWithString:@"http://feeds.feedburner.com/yummydietfood?format=xml"];
feedParser = [[MWFeedParser alloc] initWithFeedURL:feedURL];
feedParser.delegate = self;
feedParser.feedParseType = ParseTypeFull; // Parse feed info and all items
feedParser.connectionType = ConnectionTypeAsynchronously;
[feedParser parse]; 
#pragma mark -
#pragma mark MWFeedParserDelegate

- (void)feedParserDidStart:(MWFeedParser *)parser {
    //[UIApplication sharedApplication].networkActivityIndicatorVisible=YES;
    NSLog(@"Started Parsing: %@", parser.url);
}

- (void)feedParser:(MWFeedParser *)parser didParseFeedInfo:(MWFeedInfo *)info {
    NSLog(@"Parsed Feed Info: “%@”", info.title);
    //self.title = info.title;
}

- (void)feedParser:(MWFeedParser *)parser didParseFeedItem:(MWFeedItem *)item {
    NSLog(@"Parsed Feed Item: “%@”", item.title);
    if (item) [parsedItems addObject:item]; 
}

- (void)feedParserDidFinish:(MWFeedParser *)parser {
    NSLog(@"Finished Parsing%@", (parser.stopped ? @" (Stopped)" : @""));
    self.itemsToDisplay = [parsedItems sortedArrayUsingDescriptors:
                           [NSArray arrayWithObject:[[[NSSortDescriptor alloc] initWithKey:@"date" 
                                                                                 ascending:NO] autorelease]]];

    [UIApplication sharedApplication].networkActivityIndicatorVisible=YES;

    //[self performSelector:@selector(loadData)];
    [self performSelector:@selector(loadDataWithOperation)];

}

- (void)feedParser:(MWFeedParser *)parser didFailWithError:(NSError *)error {
    NSLog(@"Finished Parsing With Error: %@", error);

    UIAlertView *erroralert = [[UIAlertView alloc]initWithTitle:@"Error!" message:@"Problem connecting server, try again in few minutes" delegate:nil cancelButtonTitle:@"Okay" otherButtonTitles:nil];
    [erroralert show];
    [erroralert release];
    self.itemsToDisplay = [NSArray array];
    [parsedItems removeAllObjects];
    self.tbl.userInteractionEnabled = YES;

    [self.tbl reloadData];
}

and where you need the data parsed by the parser called this line code

if (item) {
    NSString *itemTitle = item.title ? [item.title stringByConvertingHTMLToPlainText] : @"[No Title]";


    lbl.text = itemTitle;
}
else {
    lbl.text=@"No Title";
}


Check your encoding; bottom-line is you're probably getting the HTML in UTF-8 and the string in ISO-8859-1.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜