#!/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 } } }