in reply to modifying xml file and saving them in the same file using Perl and XML:Twig

UPDATE: This has been cross-posted, and it looks like the solution is to use print():

http://stackoverflow.com/questions/29795171/modifying-xml-file-and-saving-them-in-the-same-file-using-perl-and-xmltwig#29796073

Ignore this...

I looked at the tests on CPAN (http://cpansearch.perl.org/src/MIROD/XML-Twig-3.49/t/test_3_26.t), and they use flush():

use warnings; use strict; use XML::Twig; my $twig = XML::Twig->new( pretty_print => 'indented', twig_handlers => { jdk => sub { $_->set_text('JDK 1.8.0_40')->flush(); }, }, ); $twig->parsefile_inplace( 'nightly.xml', '.bak' ); __END__ <jdk>JDK 1.8.0_40</jdk> <jdk>JDK 1.8.0_40</jdk>

I got it to create a backup (nightly.xml.bak) properly, and the original nightly.xml file was modified, but it duplicates the line for me. I am using an older version of XML::Twig, 3.37. I didn't see any open bug reports on this topic.

  • Comment on Re: modifying xml file and saving them in the same file using Perl and XML:Twig
  • Download Code

Replies are listed 'Best First'.
Re^2: modifying xml file and saving them in the same file using Perl and XML:Twig
by choroba (Cardinal) on Apr 22, 2015 at 15:33 UTC
    Using XML::XSH2, a wrapper around XML::LibXML:
    open nightly.xml ; xinsert text 'JDK 1.8.0_40' replace //jdk/text() ; save :b ;
    لսႽ† ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ