The child_nodes
method is used to find all the child nodes for the given element. This method is provided by the Mojo::DOM
module, which is an HTML/XML DOM parser with CSS selectors.
It is helpful while fetching the first child node or last child node for the given element.
$dom->at('html element')->child_nodes
This method returns the Mojo::Collection
, which contains each child node as a Mojo::DOM
object.
A sample HTML code is given below.
<div>Inside div<p id="a">Inside first paragraph </p><p id="b">Inside second paragraph</p></div>
If we find the first node for the given element, div
, using the child_nodes
method, we get the following output:
Inside div
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>');# Get first child elementsay $dom->at('div')->child_nodes->first;# Get last child elementsay $dom->at('div')->child_nodes->last;
Mojo::DOM
module.$dom
.child_nodes
method to find the first node for the given element div
, and print its Mojo::DOM
object.child_nodes
method to find the last node for the given element div
, and print its Mojo::DOM
object.