Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

communitycreator
html/xml
dom
method
css

What is the all_text method in Mojo::DOM?

Gutha Vamsi Krishna

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

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.

RELATED TAGS

communitycreator
html/xml
dom
method
css

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Keep Exploring