in reply to Re: (jeffa) Re: XML Search and Replace
in thread XML Search and Replace

Here is another way, using XML::Simple, which changes a few of the opponents, like you wanted.
use strict; use XML::Simple; my @data = (<DATA>); my $xml = XMLin((join'', @data)); foreach my $foo (@{$xml->{'foo'}}) { foreach my $opponent (@{$foo->{'opponent'}}) { if($opponent eq 'wolf') { $opponent = 'Heinz Sielmann'; } elsif($opponent eq 'ant') { $opponent = 'Peter Scott'; } } } print XMLout($xml, rootname => 'xml'); __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>
I don't usually code a lot of XML, but when I need to, I find that XML::Simple (together with Data::Dumper) often lets me do simple stuff really quickly. Of course, it takes a little tounge-in-cheek for the dereferencing sometimes, see references quick reference for an excellent tutorial on this. :)
You have moved into a dark place.
It is pitch black. You are likely to be eaten by a wolf.

Replies are listed 'Best First'.
Re: Re: Re: (jeffa) Re: XML Search and Replace
by IlyaM (Parson) on Jun 13, 2002 at 08:32 UTC
    Note that reading and writing modifed XML document using XML::Simple really works only in simple cases. The problem with XML::Simple is that XMLout(XMLin($xml)) is not guarantied to produce XML document with same structure. From perldoc XML::Simple:
    o The API offers little control over the output of "XMLout()". In particular, it is not especially likely that feeding the output from "XMLin()" into "XMLout()" will reproduce the original XML (although passing the output from "XMLout()" into "XMLin()" should reproduce the original data structure).

    --
    Ilya Martynov (http://martynov.org/)