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


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

  • Comment on Re^4: Not able to replace element value using XML::Twig

Replies are listed 'Best First'.
Re^5: Not able to replace element value using XML::Twig
by jethro (Monsignor) on Jan 29, 2010 at 03:01 UTC
    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

        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.