in reply to XML Search and Replace
I don't really understand what you are trying to do, but I think you have a problem with how you use the arguments passed to the handler (search). The handler receives 2 arguments: the twig ($orig in this case) and the current element ($search). So really you can't write $search-$gt;prev_elt( 'name' )->text eq $search, search is an XML::Twig::Elt object, not a string. Then replace is a method on an element, not on a twig, so you probably don't want to write $orig->replace( $value );.
Would this work? (I can't test it without the actual XML data) It should work provided the text of the name element before the $search element is $search, which seems a bit odd to me.
#!/bin/perl -w use strict; use XML::Twig; my( $main_file, $search, $value )= @ARGV; # get the info we need by loading the update file #my $t_upd= new XML::Twig(); #$t_upd->parsefile( $upd_file); #my $upd_badge_id = $t_upd->root->next_elt( 'badge_id')->text; #my $upd_chore = $t_upd->root->next_elt( 'jobs'); # Process the main file my $orig = new XML::Twig( TwigHandlers => { $search => \&search, }, PrettyPrint => 'indented', ); $orig->parsefile( $main_file ); $orig->flush; # don't forget or the last closing tags won't +be printed sub search { my( $orig, $search )= @_; print "hrmmm\n"; my $search_tag= $search->tag; # just replace jobs if the previous badge_id is the right one if( $search->prev_elt( 'name' )->text eq $search_tag ) { print "hrmmm\n"; $search->set_text( $value ); } $orig->flush; # print and flush memory so only one job is in th +ere at once }
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: Re: XML Search and Replace
by coreolyn (Parson) on Jun 11, 2002 at 17:01 UTC | |
by mirod (Canon) on Jun 11, 2002 at 18:00 UTC | |
by coreolyn (Parson) on Jun 11, 2002 at 18:17 UTC |