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: script and style elements 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 html
my $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 content
say $dom->at('div')->all_text;

Explanation

In the above code snippet, we see the following:

  • Line 2: We import the Mojo::DOM module.
  • 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 div and print it.
New on Educative
Learn to Code
Learn any Language as a beginner
Develop a human edge in an AI powered world and learn to code with AI from our beginner friendly catalog
🏆 Leaderboard
Daily Coding Challenge
Solve a new coding challenge every day and climb the leaderboard

Free Resources