newbie_ab has asked for the wisdom of the Perl Monks concerning the following question:

I am trying to replace one element of XML document with a new element using XML::Twig. The element value file does not change in the player.xml. I am new to this module

Here is the code snippet:

use strict; use warnings; use XML::Twig; use Data::Dumper; my $twig = XML::Twig->new(); $twig->parsefile( "player.xml"); # build the twig my $root= $twig->root; $root->set_tag( 'stats_test'); $root->print;

And the sample XML file(player.xml):

<stats> <player> <name>Houston, Allan</name> <g>69</g> <ppg>20.1</ppg> <rpg>3.4</rpg> <apg>2.8</apg> <blk>14</blk> </player> <player> <name>Sprewell, Latrell</name> <g>69</g> <ppg>19.2</ppg> <rpg>4.5</rpg> <apg>4.0</apg> <blk>15</blk> </player> <player> <name>Ewing, Patrick</name> <g>49</g> <ppg>14.6</ppg> <rpg>10.0</rpg> <apg>1.0</apg> <blk>68</blk> </player> <player> <name>Johnson, Larry</name> <g>57</g> <ppg>11.1</ppg> <rpg>5.3</rpg> <apg>2.6</apg> <blk>7</blk> </player> </stats>

Any help will be appreciated . Thanks ab

Replies are listed 'Best First'.
Re: Not able to replace element value using XML::Twig
by Anonymous Monk on Jan 24, 2010 at 23:56 UTC
    Your example code actually works as expected. Are you sure you're looking at the replacement file? It's a bit easier to do the same thing with handlers:
    my $twig = XML::Twig->new( twig_handlers => { stats => sub { $_->set_tag( 'stats_test' ) } } ); $twig->parse_file(...); $twig->flush;
    as per the documentation.

      Thanks , I ran the code snippet you had posted but got the following error : Can't locate object method "parse_file" via package "XML::Twig" at ./xml_twig_test.thpl line 14. Isn't parse_file is a function which belongs to XML::LibXML package Thanks ab

        Try parsefile instead of parse_file.

        XML::Twig is an exceptionally well-documented CPAN module, and the parsefile method is described in its POD.

        There are many ways to search for information. Just typing "xml twig parse_file" into Google returns:

        Did you mean: xml twig parsefile Top 2 results shown
Re: Not able to replace element value using XML::Twig
by jethro (Monsignor) on Jan 25, 2010 at 00:16 UTC

    Your script seems to work fine. When I ran it, the substitution took place.

    I don't know if there is a difference between $root->print and $twig->print, example code in the docs uses the latter but it didn't have any effect on my test runs. If you still have problems, you might use $twig just to be sure (even though I doubt it has something to do with it)

      Thanks , the file player.xml should be replaced with the change but it is not. I am able to see the change using $twig->print. Hope that makes sense.

        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