6

Can anyone recommend me a java library to allow me XPath Queries over URLs? I've tried JAXP without success.

Thank you.

3
  • See stackoverflow.com/questions/9022140/… - not quite a duplicate as it asks about specific XPath functionality but there are better answers there. Commented Jan 7, 2013 at 0:34
  • @Reonarudo I am in the same situation as you were when you asked this question. There are many possible suggestions/solutions in the answers, but I would like to know which solution(library) you used and did it work out the way you wanted it ? Commented Jun 20, 2015 at 19:08
  • @UtherPendragon I'm sorry but this was a long time ago and I cannot recall which project was this. Anyway there should be newer/better libraries available nowadays. Commented Jun 23, 2015 at 12:14

5 Answers 5

8

There are several different approaches to this documented on the Web:

Using HtmlCleaner

Using Jericho

I have tried a few different variations of these approaches, i.e. HtmlParser plus the Java DOM parser, and JSoup plus Jaxen, but the combination that worked best is HtmlCleaner plus the Java DOM parser. The next best combination was Jericho plus Jaxen.

Sign up to request clarification or add additional context in comments.

3 Comments

Note that on Android 4.2.2 HtmlCleaner 2.5 turned out to be 4x slower compared to jSoup 1.7.2.
Note that HtmlCleaner only supports XPath 1.0.
HTML Cleaner + DOM Serializer + Threading = Really bad memory leak
6

jsoup, Java HTML Parser Very similar to jQuery syntax way.

5 Comments

I'm not sure. It does much simpler queries, which xpath based. you can read some documentation and there are a lot of cool examples, explaining how to run that queries.
jsoup (at least in version 1.7.3) doesn't suppport XPath.
jsoup use css/jQuery syntax way ,which is similar as and better than XPath
CSS Selectors are not better than XPath. There are some things which you can select in XPath but not CSS Selectors
jsoup now supports xpath, as well as CSS selectors. Since September 2021 in jsoup 1.14.3.
2

Use Xsoup. According to the docs, it's faster than HtmlCleaner. Example

 @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));
    }

Link to Xsoup - https://github.com/code4craft/xsoup

Comments

1

You could use TagSoup together with Saxon. That way you simply replace any XML SAX parser used with TagSoup and the XPath 2.0 or XSLT 2.0 or XQuery 1.0 implementation works as usual.

Comments

0

I've used JTidy to make HTML into a proper DOM, then used plain XPath to query the DOM.

If you want to do cross-document/cross-URL queries, better use JTidy with XQuery.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.