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

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

Replies are listed 'Best First'.
Re^6: Not able to replace element value using XML::Twig
by newbie_ab (Novice) on Jan 29, 2010 at 21:18 UTC

    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

      If I execute above script, I get an error because print_to_file needs a filename as parameter, but after adding that, all runs as expected. So maybe you have a very old twig version (mine seems to be from 2007-11-13). Is print_to_file() mentioned in the man page of Twig? Can you find the subroutine print_to_file if you simply search the file XML/Twig.pm with grep (should be somewhere in /usr/lib/perl5 if you have a normal linux distribution)

      If not, just get a newer version from CPAN.

        I was able to locate Twig.pm and it does have the print_to_file function.

        sub print_to_file { my( $t, $filename)= (shift, shift); open( TWIG_PRINT_TO_FILE, ">$filename") or croak "cannot create fi +le $filename: $!"; $t->print( \*TWIG_PRINT_TO_FILE, @_); close TWIG_PRINT_TO_FILE; return $t; }

        Twig_pm.slow,v 1.303 2006/05/26 08:07:14
        This is the Twig version in my system but the code is available in the library