What is the all_text method in Mojo::DOM?
Overview
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.
Syntax
$dom->at('html element')->all_text
Note:
scriptandstyleelements are excluded.
Returns
This will return the extracted text content from the descendant nodes.
Let’s look at an example.
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
Code
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;
Explanation
In the above code snippet, we see the following:
- Line 2: We import the
Mojo::DOMmodule. - Line 5: We parse the HTML and store it in the scalar
$dom. - Line 8: We extract the text content from descendant nodes of the element
divand print it.