2011-09-11 19 views
0

我需要找到我指定的屬性不存在的元素。喜歡的東西:如何在jsoup中搜索指定屬性不存在的元素?

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

或類似的東西一樣:

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

據我所知本身jsoup不支持的XPath,所以這是不可能的。

也許有一些技巧可以實現這一點?

回答

1

解決此問題的一種方法是使用:not選擇器。下面是一個沒有id而選擇全部divs的示例。

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")); 
} 
+0

謝謝。它確實有助於解決問題。 – Eugene

+0

非常好。 –