Edit HTML Document – Java

Document Object Model

The Document Object Model, or DOM for short, is a standard cross-platform programming API that helps programmers access and modify parts of a document. DOM defines the structure of a document as a tree with a hierarchy of nodes, where each node represents a part of the document, such as an element, class, tag, attribute, or text. For example, each piece, such as an image or piece of text, is called a “node.” A DOM tree is how or with what structure a document is represented in memory. In other words, the Document Object Model creates a logical document structure and defines objects, properties, events, and methods to access and modify them.

Edit HTML using Java

HTML DOM defines HTML elements as objects, providing a set of properties and methods that you can use to access and manage them. Each element in an HTML document is represented by a node in the DOM tree, and each node has its own set of properties and methods.

As we already mentioned in the article Create HTML Document the implementation of HTMLDocument as well as the whole DOM are based on WHATWG DOM standard. So, it is easy to use Aspose.HTML having a basic knowledge of HTML and JavaScript languages. The DOM package is represented with the following fundamental data types:

Data typeDescription
DocumentThe Document class represents the entire HTML, XML or SVG document. Conceptually, it is the root of the document tree and provides the primary access to the document’s data.
EventTargetThe EventTarget class is implemented by all Nodes in an implementation that supports the DOM Event Model.
NodeThe Node class is the primary datatype for the entire Document Object Model. It represents a single node in the document tree.
ElementThe element type is based on node and represents a base class for HTML, XML or SVG DOM.
AttrThe Attr class represents an attribute in an Element object. Typically the allowable values for the attribute are defined in a schema associated with the document.

DOM Methods

HTML DOM defines a set of methods that can be used to access and control all HTML elements. You can use these methods to perform various tasks, such as creating, modifying, and deleting elements, and managing their properties and events. The following is a brief list of useful API methods provides by the core data types:

document.getElementById(elementId)The method, when invoked, must return the first element whose ID is elementId and null if there is no such element otherwise.
document.getElementsByTagName(name)The method must return the list of elements with the given name.
document.createElement(tagName)The method creates the HTML element specified by tagName, or an HTMLUnknownElement if tagName isn’t recognized.
parentNode.appendChild(node)The method adds a node to the end of the list of children of a specified parent node.
element.setAttribute(name, value)Sets the value of an attribute on the specified element.
element.getAttribute(name)The method returns the value of a specified attribute on the element.
element.innerHTMLReturns a fragment of markup contained within the element.

There are many ways you can edit HTML by using our library. You can modify the document by inserting new nodes, removing, or editing the content of existing nodes. If you need to create a new node, the following methods are ones that need to be invoked:

document.CreateCDATASection(data)Creates a CDATASection node whose value is the specified string.
document.CreateComment(data)Creates a Comment node given the specified string.
document.CreateDocumentFragment()Creates an empty DocumentFragment object.
document.CreateElement(localName)Creates an element of the type specified.
document.CreateEntityReference(name)Creates an EntityReference object.
document.CreateProcessingInstruction(target, data)Creates an ProcessingInstruction with the specified name and data.
document.CreateTextNode(data)Creates a Text node given the specified string.

Once you have new nodes are created, there are several methods in DOM that can help you to insert nodes into the tree. The following list describes the most common way of inserting nodes:

node.InsertBefore(node, child)Inserts the node before the reference child node
node.AppendChild(node)Adds the node to the list of children of the current node

To remove a node from the HTML DOM tree, please use the RemoveChild(child) or Remove() methods.

For a complete list of interfaces and methods represented in the DOM package please visit API Reference Source.

Edit HTML Document Tree

The following code snippet shows how to edit an HTML document using DOM tree and the mentioned above functional. Consider simple steps to create and edit HTML. We create HTML from scratch. The document will contain a styled text paragraph:

  1. Create an instance of an HTML document using HTMLDocument() constructor.
  2. Create a <style> element using createElement("style") method.
  3. Call the setTextContent() method to set the specified text content within the style element. The text content .gr { color: green } is a CSS rule. It targets elements with the class name “gr” and sets their color to green.
  4. Use the getElementsByTagName(name) method to find the <head> element and append the style element as a child to the head element.
  5. Create a paragraph element with class-name “gr” using createElement("p") and setClassName("gr") methods.
  6. Create a text node and add it as a child to the <p> element – use the createTextNode() and appendChild() methods.
  7. Add the paragraph to the document body.
  8. Save the HTML document to a file using save() method.
 1// Create an instance of an HTML document
 2com.aspose.html.HTMLDocument document = new com.aspose.html.HTMLDocument();
 4// Create a style element and assign the green color for all elements with class-name equals 'gr'.
 5Element style = document.createElement("style");
 6style.setTextContent(".gr { color: green }");
 8// Find the document header element and append the style element to the header
 9Element head = document.getElementsByTagName("head").get_Item(0);
12// Create a paragraph element with class-name 'gr'.
13HTMLParagraphElement p = (HTMLParagraphElement) document.createElement("p");
16// Create a text node
17Text text = document.createTextNode("Hello, World! I can edit HTML documents!");
19// Append the text node to the paragraph
22// Append the paragraph to the document body element
25// Save the HTML document to a file

The resulting HTML file looks like this:

2	<head>
3		<style>.gr { color: green; }</style>
4	</head>
5	<body>
6		<p class="gr">Hello, World! I can edit HTML documents!</p>
7	</body>

setInnerHTML() & getOuterHTML() methods

Having DOM objects gives you a powerful tool to manipulate with an HTML Document. However, sometime much better to work just with Class String. The following example demonstrates how to create an HTML document using the Aspose.HTML Java library: set the body element’s content and output the HTML document to the console using setInnerHTML() and getOuterHTML() methods:

  1. Create an instance of the HTMLDocument class using the HTMLDocument() constructor. It creates an empty HTML document.
  2. To output the original content of the HTML document to the console, use the getOuterHTML() method. The output will be <html><head></head><body></body></html> since the document is initially empty.
  3. Use the setInnerHTML() method to set the content of the body element: add an HTML <p> element with the text “Hello World!!” to the body element.
  4. Print the updated content of the HTML document to the console using the getOuterHTML() method. The result is <html><head></head><body><p>Hello World!!</p></body></html> because the body element now contains the added paragraph.
 1// Create an instance of an HTML document
 2com.aspose.html.HTMLDocument document = new com.aspose.html.HTMLDocument();
 4// Write the content of the HTML document into the console output
 5System.out.println(document.getDocumentElement().getOuterHTML()); // output: <html><head></head><body></body></html>
 7// Set the content of the body element
 8document.getBody().setInnerHTML("<p>Hello World!!</p>");
10// Write the content of the HTML document into the console output
11System.out.println(document.getDocumentElement().getOuterHTML()); // output: <html><head></head><body><p>Hello World!!</p></body></html>

Edit CSS

Cascading Style Sheets ( CSS) is a style sheet language used for describing how webpages look in the browser. Aspose.HTML not only support CSS out-of-the-box but also gives you instruments to manipulate with document styles just on the fly before converting the HTML document to the other formats.

When CSS is written using the style attribute inside of an HTML tag, it’s called an “inline style CSS”. The Inline CSS gives you to apply an individual style to one HTML element at a time. You set CSS to an HTML element by using the style attribute with any CSS properties defined within it. In the following code snippet, you can see how to specify CSS style properties for an HTML <p> element:

 1// Create an instance of an HTML document with specified content
 2String content = "<p> Inline CSS </p>";
 3com.aspose.html.HTMLDocument document = new com.aspose.html.HTMLDocument(content, ".");
 5// Find the paragraph element to set a style attribute
 6HTMLElement paragraph = (HTMLElement) document.getElementsByTagName("p").get_Item(0);
 8// Set the style attribute
 9paragraph.setAttribute("style", "font-size: 250%; font-family: verdana; color: #cd66aa");
11// Save the HTML document to a file
14// Create the instance of the PDF output device and render the document into this device
15PdfDevice device = new PdfDevice("edit-inline-css.pdf");

In this particular example, color, font-size and font-family apply to the <p> element. The fragment of rendered pdf page looks like this:

Text “Inline CSS”

You can download the complete examples and data files from GitHub.

Subscribe to Aspose Product Updates

Get monthly newsletters & offers directly delivered to your mailbox.