Introduction

The DOM layer is the core of Aspose.HTML FOSS for Python. It models an HTML page the way the standards do — as a tree of Document, Element, Text, Comment, and DocumentFragment nodes — and gives you the operations to build, inspect, and rearrange that tree from Python code, without a browser in the loop.

This post walks through the main DOM features: creating nodes, managing attributes, moving subtrees around with Node operations, parsing fragments, and looking elements up again once the tree is built. Everything shown here ships in the aspose-html-foss package on PyPI, is MIT licensed, and runs in pure Python on version 3.10 or later.


Key Features

Create Documents and Elements

Document is the node factory: create_element() for elements, create_text_node() for text, create_comment() for comments, and create_document_fragment() for lightweight containers. New nodes attach to the tree with append_child():

from aspose_html.dom import Document
doc = Document()
el = doc.create_element("div")
doc.append_child(el)

Manage Attributes

Attributes round-trip through Element.set_attribute(), get_attribute(), has_attribute(), and remove_attribute(). The Attr class models individual attribute nodes when you need them as objects rather than strings:

from aspose_html.dom import Document
doc = Document()
el = doc.create_element("div")
el.set_attribute("class", "foo")
el.set_attribute("id", "bar")
doc.append_child(el)

Tree Operations on Node

Every node inherits the standard tree toolkit from Node: insert_before(), remove_child(), replace_child(), clone_node(), and contains() for structure changes and checks, plus child_nodes, parent_node, and first_child for traversal, and text_content for reading or assigning the text of a subtree. get_root_node() and has_child_nodes() answer the common structural questions directly.

Parse Fragments and Adjacent Markup

HTMLDocument.parse() handles complete pages and HTMLDocument.parse_fragment() handles partial markup. For surgical insertion relative to an existing element, Element provides insert_adjacent_html(), insert_adjacent_text(), and insert_adjacent_element() — the standard four-position insertion API. Document.import_node() and adopt_node() move nodes between documents.

Look Up and Read Back

Document.get_element_by_id() retrieves an element by its id attribute. Combined with computed-style access from the CSSOM layer, you can verify what a subtree contains and how it will be styled:

from aspose_html.dom import Document
from aspose_html.cssom import CSSStyleSheet
doc = Document()
el = doc.create_element("div")
el.set_attribute("id", "bar")
doc.append_child(el)
sheet = CSSStyleSheet()
sheet.replace_sync("#bar { color: blue }")
doc.attach_style_sheet(sheet)
style = el.get_computed_style()
print(style.get_property_value("color"))

Quick Start

Install from PyPI:

pip install aspose-html-foss

Build a small tree, tag it with attributes, and confirm the structure:

from aspose_html.dom import Document

doc = Document()
container = doc.create_element("div")
container.set_attribute("id", "content")
doc.append_child(container)

child = doc.create_element("p")
container.append_child(child)

print(container.has_child_nodes())
print(doc.get_element_by_id("content").get_attribute("id"))

Open Source & Licensing

Aspose.HTML FOSS for Python is MIT licensed — free for commercial use, modification, and redistribution. The code lives on GitHub, and releases are published to PyPI as aspose-html-foss.


Getting Started