in reply to Re^2: Not able to replace element value using XML::Twig
in thread Not able to replace element value using XML::Twig

Ahh, that's the problem. You have to tell Twig explicitely to save it to a file (after all, a Twig user might not want to change the xml but just parse it, or print it to the screen).
When I add
$twig->print_to_file("player.xml");

at the end of your script instead of the "$root->print;" line, the file on disk gets updated too

Replies are listed 'Best First'.
Re^4: Not able to replace element value using XML::Twig
by newbie_ab (Novice) on Jan 28, 2010 at 22:39 UTC


    Thanks , I did try with print_to_file but my code gives an error that cannot locate the function for object.
    Also even though I am creating an object of type XML::Twig but the object is being created of type XML::Twig::Elt which is a subclass of XML::Twig.
    That's another reason the function is not being located by the created object

      my $twig = XML::Twig->new(); print ref $twig,"\n"; $twig->parsefile("player.xml"); my $root= $twig->root; print ref $root,"\n"; # prints: XML::Twig XML::Twig::Elt

      It seems you are still using $root for the method call instead of $twig. If I use $root->print_to_file instead of $twig->print_to_file, I too get the error message that the function can't be found

        Here is the code snippet which I am currently using

        use strict; use warnings; use XML::Twig; use Data::Dumper; my $twig = XML::Twig->new(twig_roots => { name => 1}); $twig->parsefile( "player.xml"); $twig->print; $twig->print_to_file();

        Here are the outputs :
        $ ./xml_twig_test.thpl Can't locate object method "print_to_file" via package "XML::Twig" at ./xml_twig_test.thpl line 22.
        <stats><name>Houston, Allan</name><name>Sprewell,
        Latrell</name><name>Ewing, Patrick</name><name>Johnson,
        Larry</name></stats>
        This is sort of weird since the other methods of XML::Twig are working fine except print_to_file() function