Hello,

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.

... after that, the script return error. Please help suggest. Thanks.
#!/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; }
The current XML file is:
<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 } } }

In reply to Use XML::Twig to manipulate XML data by sureerat

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.