XML-Twig - Re: Modify a compex XML documaent with XML::Twig

Posted on Sat Jun 23 08:39:09 2007 by mirod in response to 5514 (See the whole thread of 4)
Re: Modify a compex XML documaent with XML::Twig

I don't know exactly what you want to do, so it's a little difficult to give you a specific answer, but here is a piece of code that will hopefully be of some help. It locates the proper comment through its Prompt sibling, then perfoems a string substitution on it. Alternatively you could replace completely the CDATA section in the element by doing $response->set_cdata( 'your text here').

Note that there might be a bug in the released version of the module (the root start tag might be output twice, at least that was the case with the development version of XML::Twig) so you might want to grab the latest bestest version (dated June 23) from xmltwig.com

#!/usr/bin/perl use strict; use warnings; use XML::Twig; XML::Twig->new( twig_roots => { Comments => \&comments }, twig_print_outside_roots => 1, keep_spaces => 1, ) ->parsefile( 'resume.xml'); exit; sub comments { my( $t, $comments)= @_; if( $comments->field( 'Prompt')=~ m{Briefly describe your qualifications for this position}) { my $response= $comments->first_child( 'Response'); if( $response) { $response->subs_text( qr{some_company}, 'The Bestest Company Evar'); } } $t->flush; }
Direct Responses: 5520 | 5523 | Write a response