I would like to extract the text from each visited node but while traversing and build a list.
I only get an empty result.
Here is the following attempt using dom.el
(defun extract-text-from-dom (dom-node)
"Recursively extract text from a DOM-NODE."
(cond
((null dom-node) "")
((dom-text dom-node) (dom-text dom-node))
(t
(mapconcat
#'extract-text-from-dom
(dom-children dom-node)
" "))))
(let* ((html "<div><p>This is a <b>sample</b> text.</p> <br><h1>Another Text</h1></div>")
(dom-tree (libxml-parse-html-region 1 (length html) html))
(text (extract-text-from-dom dom-tree)))
(message "Extracted Text: %s" text))
libxml-parse-html-region
, the reasonextract-text-from-dom
returns an empty result is because of the(dom-text dom-node)
check. This will return a non-nil
empty string""
for any parent nodes that do not contain text, thus ending the recursion early. Have you tried usingdom-texts
fromdom.el
instead?