Thread

Posted on Sat Jun 23 03:44:11 2007 by dbmathis
Modify a compex XML documaent with XML::Twig
Temporarily removed
Direct Responses: 5515 | Write a response
Posted on Sat Jun 23 08:39:09 2007 by mirod in response to 5514
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
Posted on Sat Jun 23 17:29:23 2007 by dbmathis in response to 5515
Re: Modify a compex XML documaent with XML::Twig
Hi mirod,

Thank you for the information! After looking over the code example, it seems to be very natural and makes perfect sense. I should be able to use this to learn.

One more question. I notice you are using a regular expression to identify what part of the response should be substituted. I am planning on writing a function to truncate that response to 1000 characters, so is there a way I can just specify the entire value within the response cdata tags so that I can pass it into a function and truncate it?

Write a response
Posted on Sun Jun 24 01:37:30 2007 by dbmathis in response to 5515
Re: Modify a compex XML documaent with XML::Twig
This seems to do what I am looking for.. Thank you so much for your help mirod.

#!/usr/bin/perl -w 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) { my $trncd = substr($response->text, 0, 1000); $response->set_cdata( $trncd); } } $t->flush; }
Write a response