Parse div using jsoup in java

Parse h2 tag inside div using Jsoup:

package in.javadomain;

import java.io.IOException;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;

public class DivParseJsoup {

	/**
	 * Main method to parse h2 tag inside div using jsoup
	 * 
	 * @param args
	 */
	public static void main(String[] args) {
		Document doc;
		try {
			doc = Jsoup.connect("http://www.javadomain.in").get();
			// h2 is inside div so getting div first and h2 next.
			Elements h2Tag = doc.tagName("div#header-logo").getElementsByTag(
					"h2");
			System.out.println("Parsing h2 Tag using Jsoup => " + h2Tag.text());
		} catch (IOException e) {
		 System.out.println("Exception in Jsoup  " + e.getMessage());
			// e.printStackTrace();
		}
	}
}

Output:

Parsing h2 Tag using Jsoup => Proud to be a programmer

518 total views, 1 views today

Leave a Reply

Your email address will not be published. Required fields are marked *