iphone sdk 中的 HTML 解析

发布于 2024-11-03 07:34:17 字数 136 浏览 0 评论 0原文

我正在使用 git 中的 hpple 来解析 HTML。它工作正常。但是当我得到解析后的 NSString 时,我发现在这个字符串中,双引号 (") 和单引号 (') 被其他符号替换,例如 , 。我怎样才能得到正确的字符串?我试图替换这些字符,但是它不工作。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

南风几经秋 2024-11-10 07:34:17

查看此链接它解决了我的问题

https://github.com/mwaterfall/MWFeedParser

这里添加代码的步骤

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];
}

以及需要由解析器解析的数据的位置称为此行代码

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


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

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";
}
打小就很酷 2024-11-10 07:34:17

检查你的编码;最重要的是,您可能会获得 UTF-8 格式的 HTML 和 ISO-8859-1 格式的字符串。

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文