• Breaking News

    Monday, 5 October 2015

    JAVA HTML PARSER JSOUP - DISPLAY TITLE AND ALL DIV TEXT

    If We want to parse HTML in java then JSOUP provide that facility to us.. It provides a  API for extracting and manipulating data from web page.By using JSOUP we can select element,traverse document,manipulate html element etc In java code.


    JSOUP required jsoup-version.jar you can download if from jsoup.org/download

    Below is example in which we connect to www.google.com and select title, all div text in java code and print them.

    package com.kodemaker;
    import java.io.IOException;
    
    import org.jsoup.Jsoup;
    import org.jsoup.nodes.Document;
    import org.jsoup.nodes.Element;
    import org.jsoup.select.Elements;
    
    public class JSOUPTest {
     public static void main(String[] args) {
      try{
       Document doc = Jsoup.connect("http://www.google.com").get();
       System.out.println("text : " + doc.select("title").text());
       Elements divs = doc.select("div");  
       for (Element div : divs){     
        System.out.println("text : " + div.text());  
       }
      }catch (IOException e){
       e.printStackTrace();
      }  
     }
    }
    

    Above programme will display title of google page i.e. google and display all div text on console.

    No comments:

    Post a Comment