【发布时间】:2016-02-24 18:15:09
【问题描述】:
我正在尝试访问 Android 中 HTML 文件中的元素。我已经使用 volley (stringRequest) 检索了文档,现在正在尝试使用 JSOUP 解析文档。
HTML 文档中有一些代码如下:
<div class="theProducts">
<h3>
<a href="http://www.myproduct.com/myproduct.html" >
This is the product information I want to access
<img src="http://prettypictures.myproduct.com/myproduct.jpg" alt="" />
</a>
</h3>
</div>
我可以通过执行以下操作来访问文档中包含的“产品”:
Document doc = Jsoup.parse(response);
String title = doc.title();
Elements productElements = doc.getElementsByClass("theProducts");
for (Element productElement : productElements) {
//String name = productElement.attr("name");
//String content = productElement.attr("content");
}
所以,我很高兴收到了一系列 productElements。但是,我不确定如何访问我想要的特定元素(即“这是我想要访问的产品”)。我可以看到它嵌套在数组中,但它嵌套得很深。
有没有人可以向我解释要使用的正确语法。我对 DOM 模型不是很熟悉,因此有点困惑。我确实尝试过 doc.getElementsByClass(theProducts.h3) 和 (theProducts#h3) 但这些都不起作用,而是得到了 0 个结果。
我也尝试访问 outerHtml,但这会返回整个 <h3> 部分。
非常感谢任何帮助。
【问题讨论】: