【发布时间】:2012-10-05 18:10:38
【问题描述】:
我的字符串中有一个 html 数据,我只需要在其中获取段落值。下面是一个示例 html。
<html>
<head>
<title>
<script>
<div>
Some contents
</div>
<div>
<p> This is what i want </p>
<p> Select all data from p </p>
<p> Upto this is required </p>
</div>
<div>
Other html elements
</div>
那么如何使用字符串操作从段落中获取数据。
期望的输出
<Div>
<p> This is what i want </p>
<p> Select all data from p </p>
<p> Upto this is required </p>
</div>
【问题讨论】:
-
参考这个问题..(stackoverflow.com/questions/12363526/…)
-
您是想获取所有包含 p 个元素的 div 元素,还是只获取整个文档中的 p 个元素?
-
我尝试使用 xpath。但我的 html 格式不正确。所以没能成功。
-
@tranceporter:特定 div 标签中的 p 个元素。
-
如果你想要一个特定的 div 标签,那么你需要一些机制来唯一地识别它。您必须为其分配一个 id。否则,您将不得不选择所有具有 p 元素的 div。检查我的更新答案