代码之家  ›  专栏  ›  技术社区  ›  balexandre

如何在cocoa touch中处理html字符串

  •  0
  • balexandre  · 技术社区  · 15 年前

    我正在使用rss阅读器,点击 UITableViewCell 加载 <link> 无论是在 UIWebView 或者打开那个链接上的Safari。

    但我真的想学习如何将主题内容加载到应用程序中,而不是显示整个站点或跳转到safari

    在每个 <item> 有一个 <body> 标签(和 <Description> 包含相同但已编码的)包含主题内容的,如下图所示:

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

    所以,与其抓住 <链路& GT; 我在分配 <身体& . 问题是它不能正常工作:-(

    在这个例子中,我只得到第一个 <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];
        }
    }
    

    而要进入我的Webbrowser视图,我有:

    - (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> :-(
    }
    
    2 回复  |  直到 15 年前
        1
  •  0
  •   mgriepentrog    15 年前

    看看 SeismicXML 苹果公司的样品项目。它让你对如何使用 NSXMLParser ,它还被编写为在自己的线程中解析xml数据,以允许用户仍然与ui交互。如果要查找嵌套标记(即 <body> 在内部 <item> ,您将需要一个标志来指示您当前是否在 <项目& GT; 标记与否。否则您将解析所有 <身体& 标签。

    要解决第二个问题,这取决于您希望如何向用户呈现此信息。如果您希望文本样式化,则需要使用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: ,作为从 基本字符: 只会给你标签的内容,而不是标签本身。

        2
  •  0
  •   user23743    15 年前

    使用 NSXMLParser 检测 <body> </body> 标签,把它们之间的所有东西都串起来。

    现在我可以看到你的代码了,我可以看到你的bug了。当你遇到 <br /> 标记您更改 currentElement @"br" ,这意味着您停止向 currentBody .