I have this code:
Document dom = Jsoup.parse(htmlString);
Evaluator aTag = new Evaluator.Tag("a");
Evaluator linkClass = new Evaluator.Class("foo");
Evaluator hrefContains = new Evaluator.AttributeWithValueContaining("href", "abc");
I know how to use one evaluator
dom.selectFirst(aLinkClass);
But I want to get the first element from dom
which matches all 3 Evaluator
s.
How can I apply multiple Evaluator
in same select?
You could use the select method which accepts a string after parsing your evaluators to string and joining them with no delimiter for an AND link or a comma as delimiter for an OR link. Example:
Evaluator aTag = new Evaluator.Tag("a");
Evaluator linkClass = new Evaluator.Class("foo");
Evaluator hrefContains = new Evaluator.AttributeWithValueContaining("href", "abc");
String all_AND = String.join("", aTag.toString(), linkClass.toString(), hrefContains.toString());
String all_OR = String.join(",", aTag.toString(), linkClass.toString(), hrefContains.toString());
System.out.println(doc.selectFirst(all_AND).text());
Alternatively you could get all elments from your document and make the filtering on the java side yourself using for example a for loop or streams:
Evaluator all = new Evaluator.AllElements();
Optional<Element> target = doc.select(all)
.stream()
.filter(e -> e.is(aTag))
.filter(e -> e.is(linkClass))
.filter(e -> e.is(hrefContains))
.findFirst();
target.ifPresent(System.out::println);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With