如何在jsoup中搜索指定属性不存在的元素?

时间:2011-09-11 09:16:20

标签: java jsoup

我需要找到我指定的属性不存在的元素。类似的东西:

Doc.select( "some_tag[attribute=""]" );

或类似的东西:

Doc.select( "some_tag[!attribute]" );

据我所知 jsoup 不支持 xpath ,所以不可能。

也许有一些技巧可以实现这一目标?

1 个答案:

答案 0 :(得分:2)

解决此问题的一种方法是使用:not选择器。以下是选择没有divs的所有id的示例。

String url = "http://stackoverflow.com/questions/7377316/how-to-search-for-elements-where-specified-attribute-doesnt-exist-in-jsoup";
Document doc = Jsoup.connect(url).get();
//Select all divs without id
Elements divsWithoutid = doc.select("div:not([id])");
for (Element e : divsWithoutid) {
    //See ma, no id
    System.out.println("id = " + e.attr("id"));
}
相关问题