in reply to Re: How can I replace a line (tag) in an XML file?
in thread How can I replace a line (tag) in an XML file?
Thanks for the timely response, I really appreciate it. I tested your code and it worked. However, I'd like to read the XML from a .xml file and replace the GPA tag (which you've already done). When I tried the following, I was able to read from the .xml file and replace the GPA tag, however the changes did not write back to the .xml file. I was only able to print the desired output but not able to write it back to the .xml file. This is what I have so far.
use warnings; use strict; use XML::Twig; open File, 'test.xml'; sysread(FILE, my $str, -s FILE); my $t = XML::Twig->new( twig_handlers => { student => \&student, }, pretty_print => 'indented', ); $t->parse($str); $t->print($str); print "\n"; close FILE; sub student { my ($t, $elt) = @_; if ($elt->field('id') eq '002') { $elt->first_child('gpa')->set_text('B'); } }
The print $str returns the desired output (changes the second instance of John's GPA from 'C' to 'B') but I'm not sure how to write this output back to the test.xml file. Any suggestions on this?
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^3: How can I replace a line (tag) in an XML file?
by mirod (Canon) on Sep 30, 2011 at 04:24 UTC |