Jsoup Example: print form parameters

Jsoup example: print form parameters tutorial for beginners and professionals, jsoup - java html parser providing facility to parse html document by java language with examples of printing title, links, images, form elements from url.

Jsoup Example: print form parameters

Jsoup Example: print form parameters

In this example, we will print form parameters like parameter name and parameter value. To do so, we are calling getElementById() method of Document class and getElementsByTag() method of Element class.

register.html

  1. <!DOCTYPE html>  
  2. <html>  
  3. <head>  
  4. <meta charset="utf-8">  
  5. <title>Register Please</title>  
  6. </head>  
  7. <body>  
  8. <form id="registerform" action="register.jsp" method="post">  
  9. Name:<input type="text" name="name" value="sam"/><br/>  
  10. Password:<input type="password" name="password" value="sj"/><br/>  
  11. Email:<input type="email" name="email" value="sam@gmail.com"/><br/>  
  12. <input name="submitbutton" type="submit" value="register"/>  
  13. </form>  
  14. </body>  
  15. </html>  

JsoupPrintFormParameters.java

  1. import java.io.File;  
  2. import java.io.IOException;  
  3. import org.jsoup.Jsoup;  
  4. import org.jsoup.nodes.Document;  
  5. import org.jsoup.nodes.Element;  
  6. import org.jsoup.select.Elements;  
  7. public class JsoupPrintFormParameters {  
  8. public static void main(String[] args) throws IOException {  
  9.     Document doc = Jsoup.parse(new File("e:\\register.html"),"utf-8");  
  10.     Element loginform = doc.getElementById("registerform");  
  11.    
  12.     Elements inputElements = loginform.getElementsByTag("input");  
  13.     for (Element inputElement : inputElements) {  
  14.         String key = inputElement.attr("name");  
  15.         String value = inputElement.attr("value");  
  16.         System.out.println("Param name: "+key+" \nParam value: "+value);  
  17.     }  
  18. }  
  19. }  

Output:

Param name: name 
Param value: sam
Param name: password 
Param value: sj
Param name: email 
Param value: sam@gmail.com
Param name: submitbutton 
Param value: register