Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jsoup how to select a tag with multiple attributes

I have a table tag

<table width="100%" align="center"/>

And so far Jsoup provides

Document document =Jsoup.parse(htmlString);
document.select("table[width=100%],table[align=center]");

And this is OR comination i.e. if any one matches then elements are populated. In order to select table having width =100% and align =center I have done following

Elements element =document.select("table[align=center]");
element =element.select("table[width=100%]");

So what I am asking is that just like this OR combination

document.select("table[width=100%],table[align=center]");

is there any AND combination selector i.e. table having width =100% and align =center. Thanks in advance

like image 396
laaptu Avatar asked Dec 04 '12 12:12

laaptu


2 Answers

You can achieve an AND with one query by adding more terms to the selector. In this instance:

Elements tables = document.select("table[width=100%][align=center]");

works.

You can keep adding more terms to make it as precise as required, e.g. table[width=100%][align=center]:contains(text)

like image 156
Jonathan Hedley Avatar answered Oct 21 '22 06:10

Jonathan Hedley


At the moment (Jsoup 1.7.1) there no AND for selector available. But you can do this with two select()'s (like in your example #2):

Elements tables = document.select("table[width=100%]").select("table[align=center]");

You can also post a feature request: https://github.com/jhy/jsoup/issues

like image 35
ollo Avatar answered Oct 21 '22 07:10

ollo