0

I am trying to find a library like C# htmlagilitypack for java to parse HTML and select elements using XPath.

I have read about many libraries but none of them is standalone XPath selector for HTML, all the libraries that I have found require to parse HTML using their methods like htmlunit.

If someone can guide me with a simple example for XPath 2.0 or 3.0 and HTML parsing I would appreciate it.

catch23
  • 17,519
  • 42
  • 144
  • 217
Heopas
  • 69
  • 1
  • 10

1 Answers1

1

Java has support for Xpath. Usually, it used for parsing XML files. However, it should work for HTML as well.

HTML sample:

<html lang="en">
<head>
    <title>Index page</title>
</head>
<body>
<div>
    <br/>
    <h1>Hello <span id="my-demo">User!</span></h1>
    <br/>
    <img src="https://s3.amazonaws.com/acloudguru-opsworkslab/ACG_Austin.JPG" alt="photo"/>
</div>
</body>
</html>

Code snippet:

public class HtmlXpathParser {
    private DocumentBuilder builder;
    private XPath path;

    public HtmlXpathParser() throws ParserConfigurationException {
        DocumentBuilderFactory dbfactory = DocumentBuilderFactory.newInstance();
        builder = dbfactory.newDocumentBuilder();
        XPathFactory xpfactory = XPathFactory.newInstance();
        path = xpfactory.newXPath();
    }

    public Optional<String> parse(String fileName) throws SAXException, IOException, XPathExpressionException {
        File file = new File(fileName);

        Document doc = builder.parse(file);
        String result = path.evaluate("//img/@src", doc);

        return Optional.of(result);
    }

    public static void main(String[] args) throws ParserConfigurationException, XPathExpressionException, SAXException, IOException {
        HtmlXpathParser parser = new HtmlXpathParser();

        Optional<String> srcResult = parser.parse("src/main/resources/index.html");
        srcResult.ifPresent(System.out::println);
    }
}

Output:

https://s3.amazonaws.com/acloudguru-opsworkslab/ACG_Austin.JPG

It works for XPath version 1. You could use something like xpath2-parser if you will need it.

Useful references:

catch23
  • 17,519
  • 42
  • 144
  • 217
  • Thanks for providing this answer. The first issue that I see is that this code doesn't' t clean bad HTML and the second is that it doesn't support xpath 3. – Heopas Apr 23 '20 at 10:45