Im using HttpClient to access a particualr website and the response i get is in the form of an HTML. Which parser or method I should use the parser the HTML and get what I want from the response. Note: Im using HttpClient with Java
HTML Parser for response - Java
11k views Asked by Geek At
3
There are 3 answers
0
On
I would give htmlcleaner a try.
HTMLCleaner is Java library used to safely parse and transform any HTML found on web to well-formed XML. It is designed to be small, fast, flexible and independant. HtmlCleaner may be used in java code, as command line tool or as Ant task. Result of parsing is lightweight document object model which can easily be transformed to standards like DOM or JDom, or serialized to XML output in various ways (compact, pretty printed and so on).
You can use XPath
with htmlcleaner to get contents within xml/html tags.Here is a nice
example Xpath Example
0
On
Sample code with jsoup and Java8:
// Imports:
...
import java.nio.charset.StandardCharsets;
import org.apache.commons.io.IOUtils;
...
// Execute the GET request:
...
HttpClient clientGet = HttpClientBuilder.create().build();
HttpGet get = new HttpGet(url);
HttpResponse res = clientGet.execute(get);
// Use jsoup to parse the html response:
// E.g. find all links with reference to myapp:
// <a href="myapp">HelloWorldApp</a>
Document doc = Jsoup.parse(IOUtils.toString(res.getEntity().getContent(), StandardCharsets.UTF_8));
Elements links = doc.select("a[href~=myapp]");
for (Element link : links)
String appName = link.html();
...
Use jsoup.