2010-04-13 45 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