Working with the DOM
Working with the DOM
The DOM layer models an HTML page as a tree of typed nodes. Document is the entry point and node factory; Element and Node carry the manipulation and traversal API.
Creating Nodes
Document.create_element(), create_text_node(), create_comment(), and create_document_fragment() produce nodes; append_child() attaches them:
from aspose_html.dom import Document
doc = Document()
el = doc.create_element("div")
doc.append_child(el)Attributes
Element.set_attribute(), get_attribute(), has_attribute(), and remove_attribute() manage markup attributes; Attr and NamedNodeMap expose them as objects:
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
Node supplies the structural toolkit: insert_before(), remove_child(), replace_child(), clone_node(), and contains(), plus the child_nodes, parent_node, and first_child accessors and text_content for subtree text. get_root_node() and has_child_nodes() answer structural questions directly.
Lookup and Traversal
Document.get_element_by_id() retrieves elements by id. NodeList and HTMLCollection hold result sets; NodeIterator and TreeWalker (with NodeFilter) walk subtrees in document order.
Observing Mutations
MutationObserver records tree changes as MutationRecord entries, mirroring the standard observer pattern for reacting to structural edits.
Tips and Best Practices
- Create every node through the owning
Document— nodes are document-scoped; useDocument.import_node()oradopt_node()to move them between documents - Prefer
text_contentfor whole-subtree text over manualTextnode juggling - Use
DocumentFragmentto assemble batches of nodes before a singleappend_child() - Check
has_attribute()beforeget_attribute()when a missing attribute is meaningful
Common Issues
| Issue | Cause | Fix |
|---|---|---|
| Node appears in the wrong document | Node created by a different Document | Move it with import_node() or adopt_node() first |
| Attribute lookup returns nothing | Attribute never set on the element | Guard with has_attribute() |
| Subtree text is empty | Reading text_content before children attached | Attach children, then read |
FAQ
Which class creates new elements?
Document — its create_element() method is the factory for elements, with sibling factories for text, comments, and fragments.
How do I move a subtree between two documents?
Use Document.import_node() to copy or adopt_node() to transfer, then attach with append_child().
Is there an observer API for tree changes?
Yes — MutationObserver delivers MutationRecord batches describing structural and attribute changes.
API Reference Summary
| Class/Method | Description |
|---|---|
Document.create_element | Create an element node |
Document.get_element_by_id | Look up an element by id |
Element.set_attribute | Set a markup attribute |
Node.insert_before | Insert a node before a reference child |
Node.clone_node | Copy a node or subtree |
MutationObserver | Observe tree mutations |
TreeWalker | Filtered document-order traversal |