in reply to XML::XPath

Hey all, I ended up using XML::Twig for removing and re-adding the appropriate filenames. My code looks something like this:
my $t = new XML::Twig( ignore_elts => {'/bxh/datarec/filename' => 1, '/bxh/datarec/fileoffset' => 1, '/bxh/datarec/filerecordsize' => 1, }, TwigHandlers => { '/bxh/datarec' => \&add_filenames, }, pretty_print=>'indented', ); $t->parsefile($file); # build it $t->print; # output the twig sub add_filenames { my($t,$drec) = @_; my @filenames = glob "mri*.img"; foreach my $file(@filenames) { my $elt = new XML::Twig::Elt('filename',$file); $elt->paste('last_child',$drec); my $size = -s $file; $elt = new XML::Twig::Elt('fileoffset','0'); $elt->paste('last_child',$drec); $elt = new XML::Twig::Elt('filerecordsize',$size); $elt->paste('last_child',$drec); } }
Thanks for everyone's input!

Replies are listed 'Best First'.
Re: Re: XML::XPath
by mirod (Canon) on Aug 19, 2003 at 08:27 UTC

    Interesting, I had never thought about using ignore_elts to effectively delete elements. That's smart though. You just have to be sure that you don't ever have any other element in datarec. I would maybe add an extra $drec->cut_children at the beginning of add_filename, just in case. Fixed formats seem to have a natural tendency to evolve over time. ;--(

    You could get a slightly shorter, and I think nicer looking code, by using the insert_new_elt method:

    $elt = new XML::Twig::Elt('fileoffset','0'); $elt->paste('last_child',$drec);

    becomes:

        $drec->insert_new_elt( last_child => 'fileoffset', '0');