Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

Re: XML::Simple problem, or How to convert HTML to Perl and then back again.

by choocroot (Friar)
on Jul 11, 2003 at 19:19 UTC ( [id://273538]=note: print w/replies, xml ) Need Help??


in reply to XML::Simple problem, or How to convert HTML to Perl and then back again.

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.

Replies are listed 'Best First'.
Re: Re: XML::Simple problem, or How to convert HTML to Perl and then back again.
by eric256 (Parson) on Jul 12, 2003 at 00:12 UTC
    You can use

    my $tree = HTML::TreeBuilder->new(); $tree->ignore_unknown(0); # so it doesn't skip unknown $tree->xml_mode(1); # so it will catch <br /> tags i +f you like XHTML $tree->parse_file($file});
    to do it in HTML::TreeBuilder
    Eric Hodges

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://273538]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others about the Monastery: (4)
As of 2024-04-19 19:18 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found