in reply to Re: Re: XML Search and Replace
in thread XML Search and Replace

So here is how I would do it: you only need to update this one tag (possibly many times in the file), so I would use twig_roots and twig_print_outside_roots here: you just go through the file, outputting it as-is unless you find the path, in which case you use a handler to change the content of the element. This will give you the minimum memory footprint (and it's pretty simple too!).

Call this as update file.xml TraceNumQuery/Path http://foo.bar:8080/baz

#!/usr/bin/perl -w use strict; use XML::Twig; my $USAGE= "$0 <file> <path_to_update> <value>"; die $USAGE unless( @ARGV == 3); my( $file, $path, $value)= @ARGV; # $_ is set to the current element in the handler # you could also delete the element after printing it # for even less memory usage my $twig= XML::Twig->new( twig_roots => { $path => sub { $_->set_text( + $value); $_->print; } }, twig_print_outside_roots => 1, ); $twig->parsefile( $file);

Replies are listed 'Best First'.
Re: Re: Re: Re: XML Search and Replace
by coreolyn (Parson) on Jun 11, 2002 at 18:17 UTC

    Sweetness! Very cool. Thanks to everyone who muddled through trying to help me. This will do it!

    coreolyn