Jsoup how to select a tag with multiple attributes

laaptu picture laaptu · Dec 4, 2012 · Viewed 11.2k times · Source

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

Answer

Jonathan Hedley picture Jonathan Hedley · Dec 11, 2012

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)