Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

perl
mojo
communitycreator

What is the following_nodes 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 following_nodes method is used to find the sibling nodes for the given element. This method is provided by the Mojo::DOM module, which is an HTML/XML DOM parser with CSS selectors.

Note: The following method returns the sibling elements while the following_nodes method returns the sibling nodes.

Syntax

$dom->following_nodes
following_nodes

Return value

This method returns the Mojo::Collection, which contains each node as a Mojo::DOM object.

Example

A sample HTML code is provided below.

<div>
Inside div
<p id="a">Inside paragraph </p>
After paragraph
<h1>Inside h1</h1>
<h2>Inside h2</h2>
</div>
Sample HTML code

If we find the sibling nodes for the given element p using the following_nodes method, we get the following output:

After paragraph
<h1>Inside h1</h1>
<h2>Inside h2</h2>
Output

Code

Let's look at the code below:

use 5.010;
use Mojo::DOM;
# Parse the html
my $dom = Mojo::DOM->new('<div>Inside div <p id="a">Inside paragraph </p>After paragraph <h1>Inside h1</h1><h2>Inside h2</h2></div>');
# Get sibling nodes
say $dom->at('p')->following_nodes->join("\n");
Example

Explanation

In the above code snippet:

  • Line 2: We import the Mojo::DOM module.
  • Line 5: We parse the HTML and store it in the scalar $dom.
  • Line 8: We get the sibling nodes for the given element p using the method following_nodes and print the returned Mojo::DOM collection.

RELATED TAGS

perl
mojo
communitycreator

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