Question

One way to cul links with text() that equals some predefined strings is straightforward:

Elements links = document.getElementsByTag("a");               
for (Element link : links) {
    if (link.text().equals("So & so") || link.text().equals("such & such") {
        // add link.attr("href") to our container;
    }
}                   

But as the number of text() conditions grows, this approach looks less and less efficient.

Is there a better way to accomplish this in Jsoup?

Was it helpful?

Solution

This has nothing specific to do with Jsoup, but why not use a Set such as a HashSet to hold your valid Strings? Then if the set were called "validTextSet", you could quite simply and efficiently test if the text is in the set with

     if (validTextSet.contains(link.text())) {
        // add link.attr("href") to our container;
     }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top