Common HTML parsers in Java

Source: Internet
Author: User

Perhaps we often use DOM parsing HTML in our projects, but the parser described below may make it easier and easier for you to parse HTML. In addition, the DOM parsing commonly used in Javaweb is dom4j, it is in the web domain, we often use sax, pull in Android. Because they are more memory-saving. Because the entire XML tree does not need to be read into memory before processing, but by the way of input, in this process will trigger related events ... In these event callbacks we are going to do parsing work.


1, Jsoup is a Java HTML parser, you can directly resolve a URL address, HTML text content. It provides a very labor-saving API for fetching and manipulating data through dom,css and jquery-like operations. It is said to have been published based on the MIT Protocol.

Online Documentation: http://www.osctools.net/apidocs/apidoc?api=jsoup-1.6.3;http://jsoup.org/

The main functions of Jsoup are as follows: Parsing html from a URL, file or string, using a DOM or CSS selector to find, fetching, and manipulating HTML elements, attributes, and text;


Sample code: Document doc = jsoup.parse (input, "UTF-8", "http://www.dangdang.com");
Element content = Doc.getelementbyid ("content");
Elements links = Content.getelementsbytag ("a");
for (Element link:links) {
String linkhref = link.attr ("href");
String LinkText = Link.text ();
}


2, Htmlparser is a pure Java write HTML parsing library, it does not rely on other Java library files, mainly for the transformation or extraction of HTML. It can parse HTML at a high speed, and it won't go wrong. The latest version of Htmlparser is now 2.0.  It is said that Htmlparser is currently the best tool for parsing and analyzing HTML. Whether you want to crawl the Web page data or transform the content of the HTML, using the Htmlparser will not be able to resist praise. Online Documentation: Http://www.osctools.net/apidocs/apidoc?api=HTMLParser http://htmlparser.sourceforge.net/project-info.html

Sample code:
Parser Parser = new Parser ("http://www.dangdang.com");
NodeList list = Parser.parse (null);
Node node = list.elementat (0);
NodeList sublist = Node.getchildren ();
System.out.println (Sublist.size ());


Of course, in addition to these two there are nekohtml, Jtidy, Htmlcleaner and so on. In addition, there are htmlcxx in C + +.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.