3 Examples Of Parsing Html File Inward Coffee Using Jsoup

HTML is pith of web, all the page y'all run across inwards cyberspace are HTML, whether they are dynamically generated past times JavaScript, JSP, PHP, ASP or whatever other spider web technology. Your browser truly parse HTML as well as homecoming it for you. But what would y'all do,  if y'all postulate to parse an HTML document as well as detect only about elements,  tags, attributes or banking concern friction match if a item chemical constituent exists or non from Java program. If y'all get got been inwards Java programming for only about years, I am certain y'all get got done only about XML parsing move using parsers similar DOM as well as SAX, but at that topographic point is besides practiced gamble that y'all get got non done whatever HTML parsing work. Ironically, at that topographic point are few instances when y'all postulate to parse HTML document from pith Java application, which doesn't include Servlet as well as other Java spider web technologies. To brand the affair worse, at that topographic point is no HTTP or HTML library inwards pith JDK equally 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 await at Google to detect out how to acquire value of an HTML tag inwards Java. When I needed that I was certain that at that topographic point would hold upwardly an opened upwardly origin library which volition does it for me, but didn't know that it was equally wonderful as well as characteristic rich equally JSoup. It non solely provides back upwardly to read as well as parse HTML document but besides allows y'all to extract whatever chemical constituent cast HTML file, their attribute, their CSS flat inwards JQuery style as well as besides allows y'all to modify them. You tin forcefulness out likely do anything alongside HTML document using Jsoup. In this article, nosotros volition parse as well as HTML file as well as detect out value of championship as well as heading tags. We volition besides run across representative of downloading as well as parsing HTML from file equally good equally whatever URL or cyberspace past times parsing Google's abode page inwards Java.



What is JSoup Library

Jsoup is an opened upwardly origin Java library for working alongside real-world HTML. It provides a rattling convenient API for extracting as well as manipulating data, using the best of DOM, CSS, as well as jquery-like methods. Jsoup implements the WHATWG HTML5 specification, as well as parses HTML to the same DOM equally modern browsers similar Chrome as well as Firefox do. Here are only about of the useful features of jsoup library :
  •     Jsoup tin forcefulness out scrape as well as parse HTML from a URL, file, or string
  •     Jsoup tin forcefulness out detect as well as extract data, using DOM traversal or CSS selectors
  •     Jsoup allows y'all to manipulate the HTML elements, attributes, as well as text
  •     Jsoup provides construct clean user-submitted content against a security white-list, to preclude XSS attacks
  •     Jsoup besides output tidy HTML
Jsoup is designed to bargain alongside dissimilar kinds of HTML establish inwards the existent world, which includes proper validated HTML to incomplete non-validate tag collection. One of the pith strength of Jsoup is that it's rattling robust.


HTML Parsing inwards Java using JSoup

In this Java HTML parsing tutorial, nosotros volition run across iii dissimilar representative of parsing as well as traversing HTML document inwards Java using jsoup. In get-go 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, as well as 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 as well as a div in trunk which contains an HTML form. It has input tags to capture username as well as password as well as submit as well as reset push for farther action. It's proper HTML which tin forcefulness out hold upwardly validated i.e. all tags as well as attributes are properly closed. Here is how our sample HTML file await 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 rattling uncomplicated alongside Jsoup, all y'all postulate to telephone band is static method Jsoup.parse() and transcend 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, as well as from an InputStream. You tin forcefulness out besides specify grapheme 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 equally y'all transcend inwards a non-null string, you're guaranteed to get got a successful, sensible parse, alongside a Document containing (at least) a caput as well as a trunk element. Once y'all get got a Document, y'all tin forcefulness out acquire the information y'all desire past times calling appropriate methods inwards Document as well as its nurture classes Element as well as Node.


Java Program to parse HTML Document

 all the page y'all run across inwards cyberspace are HTML 3 Examples of Parsing HTML File inwards Java using Jsoup
Here is our consummate Java programme to parse an HTML String, an HTML file download from cyberspace as well as an HTML file from local file system. In guild to run this program, y'all tin forcefulness out either purpose Eclipse IDE or y'all tin forcefulness out only use whatever IDE or ascendancy prompt. In Eclipse, it's rattling easy, only re-create this code, create a novel Java project, right click on src bundle as well as glue it. Eclipse volition get got help of creating proper bundle as well as Java origin file alongside same name, thus absolutely less work. If y'all already get got a Sample Java project, thus it's only i step. Following Java programme shows 3 examples of parsing as well as traversing HTML file. In get-go example, nosotros direct parse an String alongside html content, inwards minute representative nosotros parse an HTML file downloaded from an URL, inwards tertiary representative nosotros charge as well as parse an HTML document from local file system. In get-go as well as tertiary representative nosotros purpose parse method to acquire a Document object which tin forcefulness out hold upwardly queried to extract whatever tag value or attribute value. In minute example, nosotros purpose Jsoup.connect() with, which takes help of making connector to URL, downloading HTML as well as parsing it. This method besides returns Document object which tin forcefulness out hold upwardly used for farther querying as well as 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 upwardly origin library which allows Java developer to parse HTML * files as well as extract elements, manipulate data, alter trend using DOM, CSS as well as * 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 two - Reading HTML page from URL         Document doc;         try {             Dr. = 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 flat cast HTML element           System.out.println("Jsoup tin forcefulness out besides 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 forcefulness out besides parse HTML file direct championship : Login Page flat of div tag : simple

Good thing nearly JSoup is that it is rattling robust. Jsoup HTML parser volition brand every endeavour to create a construct clean parse from the HTML y'all provide, regardless of whether the HTML is well-formed or not. It tin forcefulness out 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 as well as body, as well as solely appropriate elements inside the head)

That's all nearly how to parse an HTML document inwards Java. Jsoup is an splendid as well as robust opened upwardly origin library which makes reading html document, trunk fragment, html string as well as direct parsing html content from spider web extremely easy. In this article, nosotros learned hot to acquire value of a item html tag inwards Java, equally inwards fist representative nosotros extracted championship as well as value of H1 tag equally text, as well as inwards tertiary representative 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 forcefulness out purpose to extract whatever HTML tag, it besides provides convenience methods similar Document.title() as well as Element.className() method to speedily acquire championship as well as CSS class. Have fun alongside Jsoup as well as nosotros volition run across 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


Sumber https://javarevisited.blogspot.com/

0 Response to "3 Examples Of Parsing Html File Inward Coffee Using Jsoup"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel