从iOS中的整个html字符串中检索html字符串的一部分

时间:2014-02-12 10:02:30

标签: html ios iphone

我想从完整的html文档中检索html字符串的一部分,例如:

"<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<meta http-equiv="Content-Style-Type" content="text/css">
<title></title>
<meta name="Generator" content="Cocoa HTML Writer">
<style type="text/css">
p.p1 {margin: 0.0px 0.0px 0.0px 0.0px; font: 20.0px '.Helvetica Neue Interface'; color:     #333333; -webkit-text-stroke: #333333}
span.s1 {font-family: '.HelveticaNeueInterface-M3'; font-weight: normal; font-style: normal; font-size: 20.00pt; font-kerning: none}
</style>
</head>
<body>
<p class="p1"><span class="s1">Lorem ipsum dolor sit amet, consectetur adipiscing elit.<span class="Apple-converted-space"> </span></span></p>
<p class="p1"><span class="s1">Mauris eget neque eu mi condimentum luctus.<span class="Apple-converted-space"> </span></span></p>
<p class="p1"><span class="s1">Maecenas eu nisl luctus, tincidunt orci at, ultrices augue.</span></p>
<p class="p1"><span class="s1"><span class="Apple-converted-space"> </span>Suspendisse sed nulla lacus. Integer<span class="Apple-converted-space">  </span>vulputate sodales.<span class="Apple-converted-space"> </span></span></p>
<p class="p1"><span class="s1">Maecenas quis faucibus risus. Sed pellentesque tortor nec leo porttitor sodales. Vivamus imperdiet et diam et pretium. Integer luctus tempus odio eget scelerisque. Curabitur adipiscing sagittis vehicula.</span></p>
</body>
</html>"

由此我想用所有封闭的标签来回复“Mauris eget neque eu mi condimentum luctus。

”。

2 个答案:

答案 0 :(得分:0)

创建一个UIWebview并将其与HTML一起提供。然后使用-stringByEvaluatingJavaScriptFromString检索HTML的元素,在这种情况下:(注意:我还没有测试过它)

NSString *someHTML = [wView stringByEvaluatingJavaScriptFromString:@"document.getElementsByClassName('s1')[1].innerHTML;"];   

答案 1 :(得分:0)

可以使用javascript。您需要使用document.body.textContent方法在网络视图中执行stringByEvaluatingJavaScriptFromString:

NSString *text = [webView stringByEvaluatingJavaScriptFromString:@"document.body.textContent;"];