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.
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.
|