How To Parse HTML in JAVA -JSOUP Examples


Parsing HTML in JAVA is very simple with jsoup library. That is a powerful library which can parse HTML documents in a very easy way.
Here are the steps of parsing HTML via jsoup library.

Step 1: Download jsoup jar from http://jsoup.org/download

Step 2: Following example will help you in using jsoup.


import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;

public class TestClass 
{
 public static void main(String args[])
{
String html = "<p>An <a href='http://example.com/'><b>example</b></a> link.</p>";
Document doc = Jsoup.parse(html);
Element link = doc.select("a").first();

String text = doc.body().text(); // "An example link"
String linkHref = link.attr("href"); // "http://example.com/"
String linkText = link.text(); // "example""
String linkOuterH = link.outerHtml(); 
        // "<a href="http://example.com"><b>example</b></a>"
String linkInnerH = link.html(); // "<b>example</b>"

System.out.println(text);
System.out.println(linkHref);
System.out.println(linkText);
System.out.println(linkOuterH);
System.out.println(linkInnerH);
    }
}

Thursday, March 17, 2011 by Matpal Dev
Categories: | 31 comments

Comments (31)

  1. how to send data to html page

  2. Many thanks i need a simple example to start: you ghave got me what i was searching for... !

  3. thnx boss it works fyn in android

  4. How to make this work in Android. I couldn't make this working in Android

  5. This comment has been removed by the author.
  6. It's crashing in Eclipse for android. could you please tell me what could be the problem then?

  7. ojoi

  8. its crashing by giving Illegal argument exception

  9. Inspiring quest there. What happened after?
    Take care!

    Also visit my website :: Building Muscle

  10. Thanks for finally writing about > "How To Parse HTML in JAVA -JSOUP Examples" < Loved it!

    Also visit my homepage ... Muscle Rev Xtreme REviews

  11. What's up Dear, are you in fact visiting this web site on a regular basis, if so afterward you will definitely obtain nice know-how.

    Look into my website - Pure GCE Diet

  12. I like the valuable info you provide in your articles.
    I will bookmark your blog and check again here frequently.
    I am quite certain I'll learn many new stuff right here! Best of luck for the next!

    my web blog ... Le derme luxe

  13. Usually I don't read post on blogs, but I would like to say that this write-up very pressured me to check out and do it! Your writing taste has been amazed me. Thanks, quite nice article.

    Take a look at my web site ... Online Home Career

  14. Hi there, just became alert to your blog through Google,
    and found that it is really informative. I am gonna watch out for brussels.
    I will be grateful if you continue this in future.
    Lots of people will be benefited from your writing.

    Cheers!

    Feel free to visit my blog post Garcinia Cambogia Diet

  15. Hi! Quick question that's entirely off topic. Do you know how to make your site mobile friendly? My weblog looks weird when browsing from my apple iphone. I'm trying to find
    a template or plugin that might be able to resolve this issue.
    If you have any suggestions, please share. Appreciate it!


    Here is my blog ... Pure Garcinia Cambogia

  16. Very nice article. I certainly appreciate this website.
    Keep it up!

    my web site: garcinia cambogia where to buy

  17. Everything is very open with a precise description of
    the challenges. It was really informative. Your site is useful.
    Many thanks for sharing!

    My page ... Garcinia Cambogia Diet

  18. What's up, yup this paragraph is truly good and I have learned lot of things from it on the topic of blogging. thanks.

    Review my homepage: Ripped Muscle Xtreme

  19. It's an remarkable post in favor of all the web users; they will take benefit from it I am sure.

    Also visit my page: AnatomyX5

  20. I'm very happy to uncover this great site. I wanted to thank you for ones time for this wonderful read!! I definitely appreciated every little bit of it and i also have you book-marked to check out new stuff on your blog.

    Feel free to surf to my web page; Green tone pro review\ Green tone pro reviews

  21. It is not my first time to visit this website, i am
    browsing this website dailly and obtain fastidious facts
    from here everyday.

    Also visit my site ... Revitalize Skin Care

  22. where should i paste the jsoup-1.7.2.jar into jre or jvm??

  23. where should i paste the jsoup-1.7.2.jar into jre or jvm??

  24. Feel Free to visit : http://dreamit-sanjay.blogspot.in/

  25. Feel Free to visit : Android code

  26. However, without employing SEO, the task is
    half done. Request whenever you can publish your own banner ad in the
    or the girl site in exchange for publishing the banner ad
    or a good advertising your kids in your site. Instead of using the exact keyword match in the anchor text, let
    it come naturally.

    my web-site high search engine ranking optimization

  27. En bonne santé,itinérants jar sur lieu en ce qui concerne tourisme compte facebook indisponible.
    tentative sur volont� pirater un compte facebook (http://www.wat.tv) gratuit en ligne.

  28. Thanks for a marvelous posting! I truly enjoyed reading it, you will be a great author.I will ensure that I bookmark your blog and will often come back in the foreseeable future.

    I want to encourage continue your great work, have a nice morning!


    My blog post Diet for weight loss for men

  29. I know this if off topic but I'm looking into starting my own blog and was wondering what all is needed to get setup?
    I'm assuming having a blog like yours would cost a pretty penny?
    I'm not very web smart so I'm not 100% positive. Any recommendations or advice would be greatly
    appreciated. Thanks

    Take a look at my weblog; ucvessel.weebly.com ()

Leave a Reply