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.
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.
Useful references:
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.