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; use Document.import_node() or adopt_node() to move them between documents
  • Prefer text_content for whole-subtree text over manual Text node juggling
  • Use DocumentFragment to assemble batches of nodes before a single append_child()
  • Check has_attribute() before get_attribute() when a missing attribute is meaningful

Common Issues

IssueCauseFix
Node appears in the wrong documentNode created by a different DocumentMove it with import_node() or adopt_node() first
Attribute lookup returns nothingAttribute never set on the elementGuard with has_attribute()
Subtree text is emptyReading text_content before children attachedAttach 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/MethodDescription
Document.create_elementCreate an element node
Document.get_element_by_idLook up an element by id
Element.set_attributeSet a markup attribute
Node.insert_beforeInsert a node before a reference child
Node.clone_nodeCopy a node or subtree
MutationObserverObserve tree mutations
TreeWalkerFiltered document-order traversal

See Also