What is the root method in Mojo::DOM?
Overview
The root method is used to get the Mojo::DOM object of the root element. This method is provided by the Mojo::DOM module, which is an HTML/XML DOM parser with CSS selectors.
This method is useful when we want to get the root element and perform operations on its Mojo::DOM object.
Syntax
$dom->root
Return values
This method will return the Mojo::DOM object of the root node.
Let’s look at an example.
Example
Consider the given HTML:
<div>
Inside div
<p id="a">Inside first paragraph </p>
<p id="b">Inside second paragraph</p>
</div>
If we try to get Mojo::DOM object of root node using the root method, we will get the following output:
<div>Inside div <p id="a">Inside first paragraph </p><p id="b">Inside second paragraph</p></div>
Code example
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>');# Display the root elementsay $dom->root;
Code explanation
In the above code snippet, we see the following:
- Line 2: We import the
Mojo::DOMmodule. - Line 5: We parse the HTML and store it in scalar
$dom. - Line 8: We print the
Mojo::DOMobject of the root element for the given HTML.