我目前正在使用 Hpple 来解析 HTML,如下所示:
TFHpple *htmlParser = [TFHpple hppleWithHTMLData:[currentString dataUsingEncoding:NSUTF8StringEncoding]];
NSString *paragraphsXpathQuery = @"//p//text()";
NSArray *paragraphNodes = [htmlParser searchWithXPathQuery:paragraphsXpathQuery];
if ([paragraphNodes count] > 0) {
NSMutableArray *tempArray = [NSMutableArray array];
for (TFHppleElement *element in paragraphNodes) {
[tempArray addObject:[element content]];
}
article.paragraphs = tempArray;
}
这样我就得到了一个段落数组,我可以用NSString *result = [myArray componentsJoinedByString:@"\n\n"];
它来将它编译成带有换行符的单个文本体。
但是,如果 html 包含标签,它们将被解释为单独的实体,并且会自行换行,所以在一天结束时,从这样的一行开始:
<p>I went to the <a href="blablabla.html">shop</a> to get some milk!</a></p>
<p>It was awesome.</p>
我明白了:
I went to the
shop
to get some milk!
It was awesome!
当然我想得到这个(忽略标签内的其他p
标签):
I went to the shop to get some milk!
It was awesome!
你能帮我吗?