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.

In reply to Re: XML::Simple problem, or How to convert HTML to Perl and then back again. by choocroot
in thread XML::Simple problem, or How to convert HTML to Perl and then back again. by Wonko the sane

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.