Analyzing your prompt, please hold on...
An error occurred while retrieving the results. Please refresh the page and try again.
The HTMLFormElement class represents a section containing input controls for submitting user information. Using Aspose.HTML for Java, you can easily fulfill the HTML forms and send data directly from your Java application. To make this process is much more comfortable, we designed the com.aspose.html.forms package. The main element of this Package is FormEditor. It allows you to find an existing form in the document, create a new from scratch, iterate through the elements, fill them up with data and surely send the information to the remote server. In this article, you will learn how to work with FormEditor object.
In this article, you will learn how to work with a FormEditor object to manipulate HTML form in the document, input controls, and submit information. For the testing purpose in this article, we use the form designed by https://httpbin.org service.
The following code snippet demonstrates how to use the Aspose.HTML for Java library to fill and submit an HTML form programmatically:
create(document, index) method to create an instance of the
FormEditor class, passing in it the HTMLDocument instance and the index of the form being edited (value 0 refers to the first form in the HTML document).submit() method of the FormSubmitter class to submit the form data to the remote server.isSuccess() method. 1// Fill out and submit an HTML form programmatically using Aspose.HTML for Java
2
3// Initialize an instance of HTML document from "https://httpbin.org/forms/post" url
4HTMLDocument document = new HTMLDocument("https://httpbin.org/forms/post");
5
6// Create an instance of FormEditor
7FormEditor editor = FormEditor.create(document, 0);
8
9// You can fill in the data using direct access to the input elements, like this:
10InputElement custname = editor.addInput("custname");
11custname.setValue("John Doe");
12
13document.save("out.html");
14
15// or this:
16TextAreaElement comments = editor.getElement(TextAreaElement.class, "comments");
17comments.setValue("MORE CHEESE PLEASE!");
18
19// or even by performing a bulk operation, like this one:
20java.util.Map<String, String> dictionary = new java.util.HashMap<>();
21dictionary.put("custemail", "john.doe@gmail.com");
22dictionary.put("custtel", "+1202-555-0290");
23
24// Create an instance of FormSubmitter
25FormSubmitter submitter = new FormSubmitter(editor);
26
27// Submit the form data to the remote server
28// If you need, you can specify user credentials and timeout for the request, etc.
29SubmissionResult result = submitter.submit();
30
31// Check the status of the result object
32if (result.isSuccess()) {
33 // Check whether the result is in json format
34 if (result.getResponseMessage().getHeaders().getContentType().getMediaType().equals("application/json")) {
35 // Print result data to console
36 System.out.println(result.getContent().readAsString());
37 } else {
38 // Load the result data as an HTML document
39 Document doc = result.loadDocument();
40 // Inspect HTML document here
41 }
42}The FormEditor and FormSubmitter classes in the Aspose.HTML for Java library offer a streamlined approach to programmatically handling HTML forms. By leveraging these classes, developers can efficiently manipulate form elements, populate data, and submit information directly from their Java applications.
Aspose.HTML offers free online Converters for converting HTML, XHTML, MHTML, EPUB, XML and Markdown files to a variety of popular formats. You can easily convert HTML to PDF, HTML to JPG, SVG to PDF, MHTML to PDF, or MD to HTML. Just select a file, choose the format to convert, and you’re done. It’s fast and completely free!
Analyzing your prompt, please hold on...
An error occurred while retrieving the results. Please refresh the page and try again.