sureerat has asked for the wisdom of the Perl Monks concerning the following question:
I'm trying to code a script to manage an XML list. My script will identify the record by an attribute named "path". If the record is not in the list, add it. If it's already in the list, just replace it.
The current XML file is:#!/usr/bin/perl -w use strict; use XML::Twig; my %links = ( "img" => "/images/Blue_Test.jpg", "alt" => "Blue Image for test", "name" => "This is a test", "title" => "Hello World. This is a test page. Please click + here for more information.", "url" => "http://localhost/this_is_a_test.htm", "dcr" => "/templatedata/test2123/test/this_is_a_test", ); my $path = "/this_is_a_test3.htm"; my $file = "s1.xml"; my $x = XML::Twig::Elt->new( record => map { XML::Twig::Elt->new( $_ => $links{$_}) } sort keys %links) ->set_att("path",$path); if(!-e $file) { my $twig = XML::Twig::Elt->new(data => $x); open (XFILE, ">$file"); $twig->print(\*XFILE); # print it close XFILE; } else { my $twig= new XML::Twig(pretty_print => "indented", twig_handlers => # player will be cal +led { record => \&record } # when each player e +lement ); $twig->parsefile("$file"); sub record { my( $twig, $record)= @_; if($record->att('path') eq $path) { $x->replace($record); } else { $x->paste( 'last_child', $record->parent); } } $twig->flush; open (XFILE, ">$file"); $twig->print(\*XFILE); # print it close XFILE; }
---------------------------------<data> <record path="/this_is_a_test.htm"> <alt>Blue Test</alt> <dcr>/templatedata/test2123/test/this_is_a_test</dcr> <img>/images/Blue_Test.jpg</img> <name>This is a test</name> <title>Hello World. This is a test page. Please click here for mor +e information.</title> <url>http://localhost/this_is_a_test.htm</url> </record> <record path="/this_is_a_test2.htm"> <alt>Blue Test</alt> <dcr>/templatedata/test2123/test/this_is_a_test</dcr> <img>/images/Blue_Test.jpg</img> <name>This is a test</name> <title>Hello World. This is a test page. Please click here for mor +e information.</title> <url>http://localhost/this_is_a_test.htm</url> </record> </data>
Hello,
Here is the note to update my post that I find the problem of my coding. This is the latest script that I think it's working. If you have any comment or suggestion to clean my script or make it better, please guide me. Thanks.
#!/usr/bin/perl -w use strict; use XML::Twig; my %links = ( "img" => "/images/Blue_Test.jpg", "alt" => "Blue Test Image", "name" => "This is a test", "title" => "Hello World. This is a test page. Please click + here for more information.", "url" => "http://localhost/this_is_a_test.htm", "dcr" => "/templatedata/test2123/test/this_is_a_test", ); my $path = "/this_is_a_test3.htm"; my $file = "s1.xml"; my $item_pasted; my $x = XML::Twig::Elt->new( record => map { XML::Twig::Elt->new( $_ => $links{$_}) } sort keys %links) ->set_att("path",$path); if(!-e $file) { my $twig = XML::Twig::Elt->new(data => $x); open (XFILE, ">$file"); $twig->print(\*XFILE); close XFILE; } else { my $twig= new XML::Twig(pretty_print => "indented", twig_handlers => { record => \&record } ); $twig->parsefile("$file"); $x->paste('last_child',$twig->root) unless($item_pasted); #$twig->flush; open (XFILE, ">$file"); $twig->print(\*XFILE); close XFILE; } sub record { my( $twig, $record)= @_; unless ($item_pasted) { my $rpath = $record->att('path'); if ($rpath eq $path) { $x->replace($record); $item_pasted=1; return } } }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Use XML::Twig to manipulate XML data
by Tanktalus (Canon) on Oct 28, 2008 at 03:10 UTC | |
by sureerat (Acolyte) on Nov 07, 2008 at 16:53 UTC |