What is the children method in Mojo::DOM?
Overview
The children method gets all child elements for the given element. This method is provided by the Mojo::DOM module— an HTML/XML DOM parser with CSS selectors.
Syntax
$dom->children
Return Value
This method will return the Mojo::Collection, which contains the Mojo::DOM objects.
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 get the children for the given HTML using the children method, the output will be as follows:
div
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>');# Get childrensay $dom->children->map('tag')->join("\n");print "Children for div element \n";#Get children for specific elementsay $dom->at('div')->children->map('tag')->join("\n");
Explanation
- Line 2: We import the
Mojo::DOMmodule. - Line 5: We parse the HTML and store it in the scalar
$dom. - Line 8: We get the children for the given HTML.
- Line 12: We get the children for the given HTML element
divusing thechildrenmethod. Then, we print them.