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:

#!/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 }
coreolyn - exposure of ignorace is so eeew ya know?

In reply to XML Search and Replace by coreolyn

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.