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



我需要找到我指定的属性不存在的元素。比如:

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

或类似的内容,如:

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

我知道原生的jsoup不支持xpath,所以这是不可能的。

也许有一些技巧可以做到这一点?

解决这个问题的一种方法是使用: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"));
}

最新更新