#!/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 called { record => \&record } # when each player element ); $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; }