jsoup:如何选择满足条件的子节点的父节点 [英] jsoup: How to select the parent nodes, which have children satisfying a condition

查看:210
本文介绍了jsoup:如何选择满足条件的子节点的父节点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下是HTML的部分(针对问题简化了):

 < a href =/ auctions?id = 4672class =auction sec> 
< div class =progress>
< div class =guarantee>
< img src =/ img / ico / 2.png/>
< / div>
< / div> < / A>
< a href =/ auctions?id = 4670class =auction>
< div class =progress>
< div class =guarantee>
< img src =/ img / ico / 1.png/>
< / div>
< / div> < / A>

我想得到的是包含拍卖ID的矢量,为此可以使用2.png图像显示(在这种情况下,ID = 4672)。如何构建Selector查询以获取此信息?



http://jsoup.org/apidocs/org/jsoup/select/Selector.html - 在这里,我只能找到如何选择孩子,而不是父母...



提供任何帮助,包括使用其他库。我已经试过Jsoup,因为它似乎是最受欢迎的。

您可以使用 parent() )方法:

  final String html =< a href = \/ auctions? id = 4672 \class = \auction sec\> \\\

+< div class = \progress\> \\\

+< div class = \guarantee \> \\\

+< img src = \/img/ico/2.png\/> \ n
+< / div> \\\

+< / div>< / a> \\\

+< a href = \ / auctions?id = 4670 \class = \auction \> \\\

+< div class = \\progress \> \\\

+< div class = \guarantee \> \\\

+< img src = \/img/ico/1.png\/> ; \\\

+< / div> \\\

+< / div>< / a>;

Document doc = Jsoup.parse(html);元素元素:doc.select(img))//选择所有'img'标签
{
元素divGuarantee = element.parent();

//获取'img'的父元素
元素divProgress = divGuarantee.parent(); //获取父母的父母等

// ...
}


Here's the part of the HTML (simplified for the question):

<a href="/auctions?id=4672" class="auction sec"> 
 <div class="progress"> 
  <div class="guarantee"> 
   <img src="/img/ico/2.png" /> 
  </div> 
 </div> </a>
<a href="/auctions?id=4670" class="auction">  
 <div class="progress"> 
  <div class="guarantee"> 
   <img src="/img/ico/1.png" /> 
  </div> 
 </div> </a>

What I want to get is the vector containing the ids of the auctions, for which the 2.png image is displayed (id=4672 in this case). How to construct the Selector query in order to obtain this?

http://jsoup.org/apidocs/org/jsoup/select/Selector.html - Here I can only find how to select the children, not the parents...

Any help appreciated, including the usage of other libraries. I've tried Jsoup because it seemed to be the most popular.

解决方案

You can use parent() method:

final String html = "<a href=\"/auctions?id=4672\" class=\"auction sec\"> \n"
        + " <div class=\"progress\"> \n"
        + "  <div class=\"guarantee\"> \n"
        + "   <img src=\"/img/ico/2.png\" /> \n"
        + "  </div> \n"
        + " </div> </a>\n"
        + "<a href=\"/auctions?id=4670\" class=\"auction\">  \n"
        + " <div class=\"progress\"> \n"
        + "  <div class=\"guarantee\"> \n"
        + "   <img src=\"/img/ico/1.png\" /> \n"
        + "  </div> \n"
        + " </div> </a>";

Document doc = Jsoup.parse(html);

for( Element element : doc.select("img") ) // Select all 'img' tags
{
    Element divGuarantee = element.parent(); // Get parent element of 'img'
    Element divProgress = divGuarantee.parent(); // Get parent of parent etc.

    // ...
}

这篇关于jsoup:如何选择满足条件的子节点的父节点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆