Venerable Bretheren ( & Bretherenesses ),
I've been able to avoid DOM, SAX and XML in general and it has now caught up to me. After reading the available information it looks like XML::Twig is the right tool for what I'm attempting to do, but that was after some quick reading. (Unkown file lengths make potential memory problems for XML::Simple and XML::Twig just looks cleaner than XML::Parser.)
I need to create a script that can dynamically change the value of elements in a well formed XML file. A calling program will supply the name of the element to search for and the value to supply to it.
I had thought this should be a piece of cake, but I'm paying a bit of a price for avoiding XML. I can do this via a regex, but as this is a script that is liable to grow in it's functionality I figure I need to stop and get a clue how to do this right.
I've stolen this snippet from a node of mirods and it pretty much exposes my ignorance. mirod's original node is a replace of XML via an XML file, I've started to re-arange variable names and eliminated the updated XML file as I'm attempting to bring in the vars from an external program:
coreolyn - exposure of ignorace is so eeew ya know?#!/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"; # just replace jobs if the previous badge_id is the right one if( $search->prev_elt( 'name' )->text eq $search ) { print "hrmmm\n"; $orig->replace( $value ); } $orig->flush; # print and flush memory so only one job is in th +ere at once }
In reply to XML Search and Replace by coreolyn
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |