32.28.1 Document Object Model
The DOM returned by libxml-parse-html-region (and the other XML parsing functions) is a tree structure where each node has a node name (called a tag), and optional key/value attribute list, and then a list of child nodes. The child nodes are either strings or DOM objects.
(body ((width . "101"))
(div ((class . "thing"))
"Foo"
(div nil
"Yes")))
function dom-node tag \&optional attributes \&rest childrenβ
This function creates a DOM node of type tag. If given, attributes should be a key/value pair list. If given, children should be DOM nodes.
The following functions can be used to work with this structure. Each function takes a DOM node, or a list of nodes. In the latter case, only the first node in the list is used.
Simple accessors:
dom-tag nodeβ
Return the tag (also called βnode name") of the node.
dom-attr node attributeβ
Return the value of attribute in the node. A common usage would be:
(dom-attr img 'href)
=> "https://fsf.org/logo.png"
dom-children nodeβ
Return all the children of the node.
dom-non-text-children nodeβ
Return all the non-string children of the node.
dom-attributes nodeβ
Return the key/value pair list of attributes of the node.
dom-text nodeβ
Return all the textual elements of the node as a concatenated string.
dom-texts nodeβ
Return all the textual elements of the node, as well as the textual elements of all the children of the node, recursively, as a concatenated string. This function also takes an optional separator to be inserted between the textual elements.
dom-parent dom nodeβ
Return the parent of node in dom.
dom-remove dom nodeβ
Remove node from dom.
The following are functions for altering the DOM.
dom-set-attribute node attribute valueβ
Set the attribute of the node to value.
dom-append-child node childβ
Append child as the last child of node.
dom-add-child-before node child beforeβ
Add child to nodeβs child list before the before node. If before is nil, make child the first child.
dom-set-attributes node attributesβ
Replace all the attributes of the node with a new key/value list.
The following are functions for searching for elements in the DOM. They all return lists of matching nodes.
dom-by-tag dom tagβ
Return all nodes in dom that are of type tag. A typical use would be:
(dom-by-tag dom 'td)
=> '((td ...) (td ...) (td ...))
dom-by-class dom matchβ
Return all nodes in dom that have class names that match match, which is a regular expression.
dom-by-style dom styleβ
Return all nodes in dom that have styles that match match, which is a regular expression.
dom-by-id dom styleβ
Return all nodes in dom that have IDs that match match, which is a regular expression.
dom-search dom predicateβ
Return all nodes in dom where predicate returns a non-nil value. predicate is called with the node to be tested as its parameter.
dom-strings domβ
Return all strings in dom.
Utility functions:
dom-pp dom &optional remove-emptyβ
Pretty-print dom at point. If remove-empty, donβt print textual nodes that just contain white-space.