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.