2010-04-13 47 views
0

我使用RSS閱讀器,並且在我點擊UITableViewCell加載<link>UIWebView或在該鏈接上打開Safari時正常工作。如何在可可觸摸中處理HTML字符串

但是我真的想學習如何加載主題內容到而不是顯示整個網站或跳轉到Safari瀏覽器

在每各<item>的RSS提要的應用程序有一個<body>標籤(幷包含一個<Description>

alt text http://www.balexandre.com/temp/2010-04-13_0953.png

因此,而不是捕捉<link>我指定的<body>:包含主題內容,如下面所示的圖像相同,但編碼)。問題是,它不能正確:-(

在這個例子中工作,我只得到了內容,直到第一<br>僅此而已。

  • 我使用的是NSString,我會在C#中使用時,應我使用的任何其他對象,有沒有更好的對象對這樣的數據使用?
  • 我應該使用UITextView加載該信息(因爲它有滾動的話)或者我應該使用UIWebView呢?

釷謝謝你。


添加

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

,並傳遞給我的web瀏覽器查看我:

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

回答

0

看看蘋果的SeismicXML示例項目。它爲您提供瞭如何使用NSXMLParser一個總體思路,並也將被寫入解析在自己的線程中的XML數據,以允許用戶仍與UI交互。如果您正在尋找嵌套標籤(即<item>內的<body>),則您需要一個標誌來告訴您目前是否在<item>標籤內。否則,您將解析所有<body>標籤。

要解決您的第二個問題,這取決於您希望如何向用戶展示此信息。如果你想要文字風格,你將需要使用一個UIWebView。否則,你可以帶出你所需要的,並且具有通過一個UITableView,或者你在Interface Builder創建自定義視圖展開。

編輯:看到你最後的評論後,你需要創建一個標誌(即insideBody),並檢查爲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]; 
    } 
} 

這裏面你可能會做同樣的didStartElement:didEndElement:,從foundCharacters:獲取數據只會給你的標籤,而不是標籤本身的內容。

+0

我正在使用NSXMLParser獲取RSS,這只是爲''標記 – balexandre 2010-04-13 08:42:19

+0

Are您使用的NSMutableString構建''標籤內的字符串?如果沒記錯,應該繼續下去,直到關閉標記(除非遇到裏面,你正在分析在別的地方標記)。 – mgriepentrog 2010-04-13 08:54:39

+0

加入我的代碼...我使用的NSMutableString: - / – balexandre 2010-04-13 09:06:36

0

使用NSXMLParser檢測<body></body>標籤,只是抓住他們之間的一切成一個字符串。現在

,我可以看到你的代碼,我可以看到你的錯誤。當您遇到<br />標籤更改currentElement@"br",這意味着你停止添加字符currentBody

+0

我已經這樣做 - 字符串不包含所有內容,直到第一個
女巫很奇怪 – balexandre 2010-04-13 08:41:08