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: