HTML is total of web, all the page y'all run into inwards mesh are HTML, whether they are dynamically generated past times JavaScript, JSP, PHP, ASP or whatever other spider web technology. Your browser truly parse HTML in addition to homecoming it for you. But what would y'all do, if y'all require to parse an HTML document in addition to discovery about elements, tags, attributes or banking concern stand upwards for if a detail chemical ingredient exists or non from Java program. If y'all direct maintain been inwards Java programming for about years, I am certain y'all direct maintain done about XML parsing operate using parsers similar DOM in addition to SAX, but at that topographic point is likewise practiced peril that y'all direct maintain non done whatever HTML parsing work. Ironically, at that topographic point are few instances when y'all require to parse HTML document from total Java application, which doesn't include Servlet in addition to other Java spider web technologies. To brand the affair worse, at that topographic point is no HTTP or HTML library inwards total JDK every bit well; or at to the lowest degree I am non aware of that. That's why when it comes to parse a HTML file, many Java programmers had to expect at Google to discovery out how to acquire value of an HTML tag inwards Java. When I needed that I was certain that at that topographic point would live on an opened upwards beginning library which volition does it for me, but didn't know that it was every bit wonderful in addition to characteristic rich every bit JSoup. It non solely provides back upwards to read in addition to parse HTML document but likewise allows y'all to extract whatever chemical ingredient cast HTML file, their attribute, their CSS cast inwards JQuery style in addition to likewise allows y'all to modify them. You tin post away in all probability do anything alongside HTML document using Jsoup. In this article, nosotros volition parse in addition to HTML file in addition to discovery out value of championship in addition to heading tags. We volition likewise run into instance of downloading in addition to parsing HTML from file every bit good every bit whatever URL or mesh past times parsing Google's dwelling line solid page inwards Java.
HTML parsing is really uncomplicated alongside Jsoup, all y'all require to telephone phone is static method Jsoup.parse() and overstep your HTML String to it. JSoup provides several overloaded parse() method to read HTML file from String, a File, from a base of operations URI, from an URL, in addition to from an InputStream. You tin post away likewise specify graphic symbol encoding to read HTML files correctly which is non inwards "UTF-8" format. Here is consummate listing of HTML parse method from JSoup library. The parse(String html) method parses the input HTML into a novel Document. In Jsoup, Document extends Element which extends Node. Also TextNode extends Node. As long every bit y'all overstep inwards a non-null string, you're guaranteed to direct maintain a successful, sensible parse, alongside a Document containing (at least) a caput in addition to a torso element. Once y'all direct maintain a Document, y'all tin post away acquire the information y'all desire past times calling appropriate methods inwards Document in addition to its raise classes Element in addition to Node.
Good thing close JSoup is that it is really robust. Jsoup HTML parser volition brand every get to create a construct clean parse from the HTML y'all provide, regardless of whether the HTML is well-formed or not. It tin post away handgrip next mistakes :
unclosed tags (e.g. <p>Java <p>Scala to <p>Java</p> <p>Scala</p>)
implicit tags (e.g. a naked <td>Java is Great</td> is wrapped into a <table><tr><td>)
reliably creating the document construction (html containing a caput in addition to body, in addition to solely appropriate elements inside the head)
That's all close how to parse an HTML document inwards Java. Jsoup is an fantabulous in addition to robust opened upwards beginning library which makes reading html document, torso fragment, html string in addition to straight parsing html content from spider web extremely easy. In this article, nosotros learned hot to acquire value of a detail html tag inwards Java, every bit inwards fist instance nosotros extracted championship in addition to value of H1 tag every bit text, in addition to inwards tertiary instance nosotros learned how to acquire value of an attribute from html tag past times extracting CSS class. Apart from powerful jQuery style html.body().getElementsByTag("h1").text() method, which y'all tin post away role to extract whatever HTML tag, it likewise provides convenience methods similar Document.title() in addition to Element.className() method to speedily acquire championship in addition to CSS class. Have fun alongside Jsoup in addition to nosotros volition run into brace of to a greater extent than examples of this API soon.
Further Reading
Introduction to Spring MVC 4
RESTFul Services inwards Java using Bailiwick of Jersey
Java Web Fundamentals
What is JSoup Library
Jsoup is an opened upwards beginning Java library for working alongside real-world HTML. It provides a really convenient API for extracting in addition to manipulating data, using the best of DOM, CSS, in addition to jquery-like methods. Jsoup implements the WHATWG HTML5 specification, in addition to parses HTML to the same DOM every bit modern browsers similar Chrome in addition to Firefox do. Here are about of the useful features of jsoup library :- Jsoup tin post away scrape in addition to parse HTML from a URL, file, or string
- Jsoup tin post away discovery in addition to extract data, using DOM traversal or CSS selectors
- Jsoup allows y'all to manipulate the HTML elements, attributes, in addition to text
- Jsoup provides construct clean user-submitted content against a prophylactic white-list, to preclude XSS attacks
- Jsoup likewise output tidy HTML
HTML Parsing inwards Java using JSoup
In this Java HTML parsing tutorial, nosotros volition run into 3 dissimilar instance of parsing in addition to traversing HTML document inwards Java using jsoup. In kickoff example, nosotros volition parse an HTML String which contents all tags inwards cast of String literal inwards Java. In Second example, nosotros volition download our HTML document from web, in addition to inwards tertiary example, nosotros volition charge our ain sample HTML file login.html for parsing. This file is a sample HTML document which contains championship tag in addition to a div in torso which contains an HTML form. It has input tags to capture username in addition to password in addition to submit in addition to reset push clitoris for farther action. It's proper HTML which tin post away live on validated i.e. all tags in addition to attributes are properly closed. Here is how our sample HTML file expect similar :<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> <title>Login Page</title> </head> <body> <div id="login" class="simple" > <form action="login.do"> Username : <input id="username" type="text" /><br> Password : <input id="password" type="password" /><br> <input id="submit" type="submit" /> <input id="reset" type="reset" /> </form> </div> </body> </html>
HTML parsing is really uncomplicated alongside Jsoup, all y'all require to telephone phone is static method Jsoup.parse() and overstep your HTML String to it. JSoup provides several overloaded parse() method to read HTML file from String, a File, from a base of operations URI, from an URL, in addition to from an InputStream. You tin post away likewise specify graphic symbol encoding to read HTML files correctly which is non inwards "UTF-8" format. Here is consummate listing of HTML parse method from JSoup library. The parse(String html) method parses the input HTML into a novel Document. In Jsoup, Document extends Element which extends Node. Also TextNode extends Node. As long every bit y'all overstep inwards a non-null string, you're guaranteed to direct maintain a successful, sensible parse, alongside a Document containing (at least) a caput in addition to a torso element. Once y'all direct maintain a Document, y'all tin post away acquire the information y'all desire past times calling appropriate methods inwards Document in addition to its raise classes Element in addition to Node.
Java Program to parse HTML Document
Here is our consummate Java plan to parse an HTML String, an HTML file download from mesh in addition to an HTML file from local file system. In guild to run this program, y'all tin post away either role Eclipse IDE or y'all tin post away only use whatever IDE or ascendance prompt. In Eclipse, it's really easy, only re-create this code, create a novel Java project, right click on src bundle in addition to glue it. Eclipse volition accept help of creating proper bundle in addition to Java beginning file alongside same name, hence absolutely less work. If y'all already direct maintain a Sample Java project, in addition to then it's only 1 step. Following Java plan shows 3 examples of parsing in addition to traversing HTML file. In kickoff example, nosotros straight parse an String alongside html content, inwards instant instance nosotros parse an HTML file downloaded from an URL, inwards tertiary instance nosotros charge in addition to parse an HTML document from local file system. In kickoff in addition to tertiary instance nosotros role parse method to acquire a Document object which tin post away live on queried to extract whatever tag value or attribute value. In instant example, nosotros role Jsoup.connect() with, which takes help of making connecter to URL, downloading HTML in addition to parsing it. This method likewise returns Document object which tin post away live on used for farther querying in addition to getting value of whatever tag or attribute.import java.io.File; import java.io.IOException; import org.jsoup.Jsoup; import org.jsoup.nodes.Document; import org.jsoup.nodes.Element; /** * Java Program to parse/read HTML documents from File using Jsoup library. * Jsoup is an opened upwards beginning library which allows Java developer to parse HTML * files in addition to extract elements, manipulate data, alter vogue using DOM, CSS in addition to * JQuery similar method. * * @author Javin Paul */ public class HTMLParser{ public static void main(String args[]) { // Parse HTML String using JSoup library String HTMLSTring = "<!DOCTYPE html>" + "<html>" + "<head>" + "<title>JSoup Example</title>" + "</head>" + "<body>" + "<table><tr><td><h1>HelloWorld</h1></tr>" + "</table>" + "</body>" + "</html>"; Document html = Jsoup.parse(HTMLSTring); String championship = html.title(); String h1 = html.body().getElementsByTag("h1").text(); System.out.println("Input HTML String to JSoup :" + HTMLSTring); System.out.println("After parsing, Title : " + title); System.out.println("Afte parsing, Heading : " + h1); // JSoup Example ii - Reading HTML page from URL Document doc; try { physician = Jsoup.connect("http://google.com/").get(); championship = doc.title(); } catch (IOException e) { e.printStackTrace(); } System.out.println("Jsoup Can read HTML page from URL, championship : " + title); // JSoup Example 3 - Parsing an HTML file inwards Java //Document htmlFile = Jsoup.parse("login.html", "ISO-8859-1"); // wrong Document htmlFile = null; try { htmlFile = Jsoup.parse(new File("login.html"), "ISO-8859-1"); } catch (IOException e) { // TODO Auto-generated grab block e.printStackTrace(); } // right championship = htmlFile.title(); Element div = htmlFile.getElementById("login"); String cssClass = div.className(); // getting cast form HTML element System.out.println("Jsoup tin post away likewise parse HTML file directly"); System.out.println("title : " + title); System.out.println("class of div tag : " + cssClass); } }
Output: Input HTML String to JSoup :<!DOCTYPE html><html><head><title>JSoup Example</title></head><body><table><tr><td><h1>HelloWorld</h1></tr></table></body></html> After parsing, Title : JSoup Example Afte parsing, Heading : HelloWorld Jsoup Can read HTML page from URL, championship : Google Jsoup tin post away likewise parse HTML file straight championship : Login Page cast of div tag : simple
Good thing close JSoup is that it is really robust. Jsoup HTML parser volition brand every get to create a construct clean parse from the HTML y'all provide, regardless of whether the HTML is well-formed or not. It tin post away handgrip next mistakes :
unclosed tags (e.g. <p>Java <p>Scala to <p>Java</p> <p>Scala</p>)
implicit tags (e.g. a naked <td>Java is Great</td> is wrapped into a <table><tr><td>)
reliably creating the document construction (html containing a caput in addition to body, in addition to solely appropriate elements inside the head)
That's all close how to parse an HTML document inwards Java. Jsoup is an fantabulous in addition to robust opened upwards beginning library which makes reading html document, torso fragment, html string in addition to straight parsing html content from spider web extremely easy. In this article, nosotros learned hot to acquire value of a detail html tag inwards Java, every bit inwards fist instance nosotros extracted championship in addition to value of H1 tag every bit text, in addition to inwards tertiary instance nosotros learned how to acquire value of an attribute from html tag past times extracting CSS class. Apart from powerful jQuery style html.body().getElementsByTag("h1").text() method, which y'all tin post away role to extract whatever HTML tag, it likewise provides convenience methods similar Document.title() in addition to Element.className() method to speedily acquire championship in addition to CSS class. Have fun alongside Jsoup in addition to nosotros volition run into brace of to a greater extent than examples of this API soon.
Further Reading
Introduction to Spring MVC 4
RESTFul Services inwards Java using Bailiwick of Jersey
Java Web Fundamentals