Warning: HTML is not XML ... unless you are dealing with XHTML ...
HTML::TreeBuilder might not work because of your non-HTML tags (the <TMPL_IF> tag), so if the document is "well formed" then I would use
XML::Twig:
use strict;
use warnings;
use XML::Twig;
my $twig = XML::Twig->new(
pretty_print => 'indented',
twig_handlers => {
TMPL_IF => sub {
my ($t, $TMPL_IF) = @_;
$TMPL_IF->set_text( 'changed it' );
}
}
);
$twig->parse(\*DATA);
$twig->print;
__DATA__
<html>
<head> <title> test </title> </head>
<body bgcolor='red' >
<errors>
<TMPL_IF NAME='INVALID_WIDGET_SIZE' > waka waka </TMPL_IF>
<TMPL_IF NAME='INVALID_WIDGET_COLOR' > waka waka waka </TMPL_IF>
</errors>
<table>
<tr colspan='2'>
<td> text </td>
<td> text2 </td>
</tr>
</table>
</body>
</html>
output:
<html>
<head>
<title> test </title>
</head>
<body bgcolor="red">
<errors>
<TMPL_IF NAME="INVALID_WIDGET_SIZE">changed it</TMPL_IF>
<TMPL_IF NAME="INVALID_WIDGET_COLOR">changed it</TMPL_IF>
</errors>
<table>
<tr colspan="2">
<td> text </td>
<td> text2 </td>
</tr>
</table>
</body>
</html>
Here,
XML::Twig is simply used as a filter, but once your XML is parsed in the
$twig object, you can easily access the elements and change their content/tag/attributes.