The to_string
method renders the given node and its contents to an HTML/XML string. This method is provided by the Mojo::DOM
module, which is an HTML/XML
my $str = $dom->to_string;
This method returns a string of HTML/XML for the given node and its contents.
Let’s take a look at an example.
In the following example, we will try to render the given node and its contents to an HTML/XML string.
use 5.010;use Mojo::DOM;# Parse the htmlmy $dom = Mojo::DOM->new('<div>Inside div <p id="a">Inside paragraph </p>After paragraph<h1>Inside h1</h1>After h1<h2>Inside h2</h2></div>');# get rendered stringmy $str = $dom->at('p')->to_string;say $str
Mojo::DOM
module.Mojo::DOM
object with the new
keyword and parse the HTML. We then store it in the $dom
p
, using the at()
method. We render the p
node and its contents to the HTML/XML string using the to_string
method.str
string returned in line 10.