Does jsoup support xpath?

JSoup doesn’t support XPath yet, but you may try XSoup“Jsoup with XPath”.

Here’s an example quoted from the projects Github site (link):

@Test
public void testSelect() {

    String html = "<html><div><a href="https://github.com">github.com</a></div>" +
            "<table><tr><td>a</td><td>b</td></tr></table></html>";

    Document document = Jsoup.parse(html);

    String result = Xsoup.compile("//a/@href").evaluate(document).get();
    Assert.assertEquals("https://github.com", result);

    List<String> list = Xsoup.compile("//tr/td/text()").evaluate(document).list();
    Assert.assertEquals("a", list.get(0));
    Assert.assertEquals("b", list.get(1));
}

There you’ll also find a list of features and expressions of XPath that are supported by XSoup.

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)