2011-04-27 64 views
0

我使用git來解析HTML。它工作正常。但是當我得到解析的NSString時,我發現在這個字符串中,雙引號(「)和單(')被其他一些符號替換,例如」我怎樣才能得到正確的字符串?我試圖替換這些字符,但是。它不工作HTML在iphone解析sdk

回答

1

看看這個鏈接它解決我的問題

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 { 
    [email protected]"No Title"; 
} 
+0

是的謝謝阿曼它的好。它正在處理我的網址。你能否給我簡單的步驟來把這個插入到我的項目中。 – Arvind 2011-04-27 13:13:18

+0

目前我使用hpple,但應用程序連續終止而不顯示任何警告或異常。 – Arvind 2011-04-27 13:22:59

+0

請檢查我已編輯的答案......... – 2011-04-28 04:30:15

0

檢查您的編碼。底線是你可能得到的UTF-8的HTML和ISO-8859-1的字符串

+0

我使用這個解析器解析的數據:NSData的* htmlData = [[NSString的stringWithContentsOfURL:[NSURL URLWithString:@「HTTP:// URL/「]] dataUsingEncoding:NSUTF8StringEncoding]; – Arvind 2011-04-27 10:30:01

+0

當我使用Hpple時,我的應用程序不斷終止。 – Arvind 2011-04-27 13:15:46