Create HTML Documents with Aspose.HTML for Java
Aspose.HTML for Java
creates and edits HTML through a standards-based Document Object Model. Initialize an
HTMLDocument
, create elements, set their text and attributes, append them to the document, and call
save()
to write the current DOM to an HTML file.
A new HTMLDocument provides the standard <html>, <head>, and <body> structure. This makes the DOM workflow suitable for reports, generated pages, email templates, and other content whose elements are assembled from application data.
Build an HTML Document with DOM Methods
The example creates an empty document, adds a <style> element to the document head, inserts a heading and paragraph into the body, and saves the result as output.html. The try-with-resources statement closes the HTMLDocument after the file is written.
Create an HTML File in Java
try (HTMLDocument document = new HTMLDocument()) {
// Add CSS to the document head
Element style = document.createElement("style");
style.setTextContent(".title { color: darkblue; }");
document.getElementsByTagName("head").get_Item(0).appendChild(style);
// Add content to the document body
Element heading = document.createElement("h1");
heading.setClassName("title");
heading.setTextContent("Quarterly report");
Element paragraph = document.createElement("p");
paragraph.setTextContent("Generated with Aspose.HTML for Java");
document.getBody().appendChild(heading);
document.getBody().appendChild(paragraph);
// Save the generated HTML file
document.save("output.html");
}
Steps to Generate an HTML File in Java
- Create an empty document with the
HTMLDocument()constructor. - Use
createElement()to create the required HTML elements. - Set element text, classes, and other attributes through the DOM API.
- Add nodes to the document head or body with
appendChild(). - Call
save()to serialize the current DOM tooutput.html.
Add CSS and Resources to Generated HTML
Create a <style> element when the CSS belongs inside the generated document, as shown in the example. For an external stylesheet, create a <link> element and set its rel and href attributes. Images, scripts, fonts, and other resources can be referenced with their standard HTML elements and attributes.
Relative resource URLs must remain valid from the saved HTML file’s location. Use absolute URLs or place the referenced files at the expected relative paths. When HTML markup is loaded from a string or stream, provide a base URL so the API can resolve relative references correctly.
HTMLDocument and Java DOM API Guides
See Create and Load HTML Documents for document constructors and source-loading examples. Edit an HTML Document explains how to create, insert, replace, and remove DOM nodes.
The
HTMLDocument API Reference
lists available constructors and save methods. Use the
Document
and
Element
references for element creation, queries, attributes, and text content.
Frequently Asked Questions
1. How do I create an HTML document from a Java string?
Use HTMLDocument(html, baseUrl), where html contains the markup and baseUrl resolves relative paths to stylesheets, images, scripts, and links. After creating or modifying the document, call save() with the output file path.
2. Should I use setTextContent() or setInnerHTML() when generating HTML in Java?
Use setTextContent() for plain text because HTML characters remain text and are not parsed as markup. Use setInnerHTML() only when trusted markup must be converted into child elements. Do not pass untrusted content to setInnerHTML() without validation or sanitization.
3. How can I generate HTML in Java without hard-coding every element?
Load an existing HTML file as a template with HTMLDocument, locate the required elements with CSS selectors, replace their text or attributes, and save the completed document under a new name. This approach separates the reusable page structure from application data.
Install Aspose.HTML for Java
Add Aspose.HTML for Java from the official Aspose repository. Replace ASPOSE_HTML_VERSION with an exact version from the
Aspose.HTML for Java releases
page, then reload or rebuild the project so the dependency is resolved.
Add the repository and dependency to the Maven project:
Maven – pom.xml
<repositories>
<repository>
<id>AsposeJavaAPI</id>
<url>https://releases.aspose.com/java/repo/</url>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>com.aspose</groupId>
<artifactId>aspose-html</artifactId>
<version>ASPOSE_HTML_VERSION</version>
</dependency>
</dependencies>Add the repository and dependency to the Gradle project:
Gradle – build.gradle
repositories {
maven {
url = uri('https://releases.aspose.com/java/repo/')
}
}
dependencies {
implementation 'com.aspose:aspose-html:ASPOSE_HTML_VERSION'
}Use a JDK compatible with the selected release. For manual JAR installation, Kotlin DSL, updates, and dependency troubleshooting, see Install Aspose.HTML for Java .
Related Java HTML Tasks
Continue with Java guides for parsing, converting, merging, and extracting content from web documents.