1. Jsoup
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
public class HtmlParserExample {
public static void main(String[] args) {
String html = "<html><head><title>Example</title></head><body><h1>Hello, World!</h1></body></html>";
Document doc = Jsoup.parse(html);
Element title = doc.select("title").first();
System.out.println("Title: " + title.text());
}
}
2. HTMLCleaner
import org.htmlcleaner.*;
public class HtmlParserExample {
public static void main(String[] args) throws XPatherException {
String html = "<html><body><p>This is a paragraph.</p></body></html>";
HtmlCleaner cleaner = new HtmlCleaner();
TagNode node = cleaner.clean(html);
Object[] paragraphNodes = node.evaluateXPath("//p");
if (paragraphNodes.length > 0) {
TagNode paragraphNode = (TagNode) paragraphNodes[0];
System.out.println("Paragraph: " + paragraphNode.getText().toString());
}
}
}