Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

Re: Replacing things in XML files

by borisz (Canon)
on Apr 26, 2006 at 19:23 UTC ( [id://545853]=note: print w/replies, xml ) Need Help??


in reply to Replacing things in XML files

For simple changes XML::Simple is a fine tool. Or use XML::Twig. As a last resort XML::LibXML is also useful.
Boris

Replies are listed 'Best First'.
Re^2: Replacing things in XML files
by derby (Abbot) on Apr 26, 2006 at 20:08 UTC

    #!/usr/bin/perl use strict; use warnings; use XML::LibXML; local $/; my $xml = <DATA>; print $xml, "\n"; my $parser = XML::LibXML->new(); my $doc = $parser->parse_string( $xml ); my $nodes = $doc->findnodes( '/msg/text()' ); my $node = $nodes->pop(); my $string = $node->data; $string =~ s/^As a last resort //; $string =~ s/is also/is extremely/; $node->setData( $string ); $node->appendData( " ... and XML::LibXSLT too!" ); print $doc->toString(); __DATA__ <?xml version="1.0" encoding="iso-8859-1"?> <msg>As a last resort XML::LibXML is also useful.</msg>

    -derby
      use strict; use XML::Twig; local $/; XML::Twig->new( TwigHandlers => { '/root/msg' => sub { $_[1]->set_text('Twig is easy') } } )->parse(<DATA>)->print; __DATA__ <?xml version="1.0" encoding="iso-8859-1"?> <root><msg>As a last resort XML::LibXML is also useful.</msg></root>
      Boris

Log In?
Username:
Password:

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

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

    No recent polls found