ios - hpple:是否有可能获得像 javascript textContent 这样的文本值

标签 ios xml-parsing hpple

hpple中是否可以递归只获取子元素的所有文本内容? TFHppleElement 类中有任何方法吗? 例如 JavaScript

document.getElementById("testdiv").textContent

最佳答案

我正在使用这段代码获取新闻标题的所有内容

NSURL *newURL = [NSURL URLWithString:@"http://somesite"];
        NSData *newsData = [NSData dataWithContentsOfURL: newURL];

        TFHpple *newsParser = [TFHpple hppleWithHTMLData: newsData];

        NSString *newsXpathQueryString = @"//div[@class='item column-1']";
        NSArray *newsNodes = [newsParser searchWithXPathQuery: newsXpathQueryString];

        NSMutableArray *newNews = [[NSMutableArray alloc] initWithCapacity: 0];

        for (TFHppleElement *element in newsNodes)
        {
            News *news = [[News alloc] init];

            [newNews addObject: news];

            news.title = [[element content] stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];

            news.photo_url = [element objectForKey:@"src"];

            _allNews = newNews;
            [self.tableView reloadData];
        }
    }

你可以使用

news.title = [[element firstChild]content] to get children elements content

关于ios - hpple:是否有可能获得像 javascript textContent 这样的文本值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29546753/

相关文章:

java - 使用 XSLT 为所有节点生成 XML 的 xpath

html - 复杂 html 的 hpple XPath 查询

android - 如果同时使用 iOS,Google Play Services 8.4.0 会破坏适用于 Android 的 Google Cloud Messaging

Interface Builder 中的 iOS Swift 3.0 自定义 View 导致重新编译和错位

ios - XCGLogger 线程安全吗?

javascript - 如何使用 jQuery 解析嵌入在 ASP 页面中的 XML

iphone - UIDatePicker 在 iPhone 上横向显示为黑色

java - SAX 查找结束标签

iOS 解析内容需要 hpple 帮助

objective-c - 使用 Hpple 解析器和 NSXMLParser 迭代解析内部 HTML