use strict; use XML::Parser; use XML::Writer; my $writer = XML::Writer->new(); my $parser = XML::Parser->new( Handlers => { Init => \&handle_Init, Start => \&handle_Start, Char => \&handle_Char, End => \&handle_End, Final => \&handle_Final, } ); # i could have also made these $parser attributes # such as $parser->{from} and $parser->{to} our $from = 'foo'; our $to = 'struggle'; my $data = do {local $/;<DATA>}; $parser->parse($data); # called once at the beginning sub handle_Init { $writer->xmlDecl('UTF-8'); $writer->doctype('xml'); } # called each time a start element is encountered sub handle_Start { my($self,$name,%atts) = @_; $name = $to if $name eq $from; $writer->startTag($name,%atts); } # called each time non-markup data is encountered sub handle_Char { my($self,$text) = @_; $writer->characters($text); } # called each time an end element is encountered sub handle_End { my($self,$name) = @_; $name = $to if $name eq $from; $writer->endTag($name); } # called once at the end of the document sub handle_Final { $writer->end(); } __DATA__ <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE xml> <xml> <foo class="life or death"> <opponent>wolf</opponent> <opponent>ant</opponent> </foo> <foo class="life or death"> <opponent>pantomime goose</opponent> <opponent>Terrance Rattigan</opponent> </foo> </xml>
jeffa
L-LL-L--L-LL-L--L-LL-L-- -R--R-RR-R--R-RR-R--R-RR B--B--B--B--B--B--B--B-- H---H---H---H---H---H--- (the triplet paradiddle with high-hat)
In reply to (jeffa) Re: XML Search and Replace
by jeffa
in thread XML Search and Replace
by coreolyn
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |