Java HTML / XML How to - Get data from a form Using JSoup








Question

We would like to know how to get data from a form Using JSoup.

Answer

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
/*from   w  w w  .j a va  2 s  . c  o  m*/
public class Main {

    public static void parseHtml(String html) {
        Document document = Jsoup.parse(html);
        Element linkElement = document.select("a").first();
        String linkHref = linkElement.attr("href"); // "http://sample.com"
        String linkText = linkElement.text(); // "This is sample"
        System.out.println(linkHref);
        System.out.println(linkText);
    }

    public static void main(String[] args) {
        String html = "<a onclick=\"xyz;\" target=\"_blank\" href=\"http://java2s.com\">This is sample</a>";
        parseHtml(html);
    }

}