The all_text
method extracts the text content from all descendant nodes for the given element. This method is provided by the Mojo::DOM
module, an HTML/XML DOM parser with CSS selectors.
$dom->at('html element')->all_text
Note:
script
andstyle
elements are excluded.
This will return the extracted text content from the descendant nodes.
Let’s look at an example.
The given HTML is as follows:
<div>
Inside div
<p id="a">Inside first paragraph </p>
<p id="b">Inside second paragraph</p>
</div>
If we extract the text content from the descendant nodes of div
, we’ll get the output as follows for the given HTML.
Inside div Inside first paragraph Inside second paragraph
use 5.010;use Mojo::DOM;# Parse the htmlmy $dom = Mojo::DOM->new('<div>Inside div <p id="a">Inside first paragraph </p><p id="b">Inside second paragraph</p></div>');# Extract the text contentsay $dom->at('div')->all_text;
In the above code snippet, we see the following:
Mojo::DOM
module.$dom
.div
and print it.