#!/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;
}
####
Blue Test
/templatedata/test2123/test/this_is_a_test
/images/Blue_Test.jpg
This is a test
Hello World. This is a test page. Please click here for more information.
http://localhost/this_is_a_test.htm
Blue Test
/templatedata/test2123/test/this_is_a_test
/images/Blue_Test.jpg
This is a test
Hello World. This is a test page. Please click here for more information.
http://localhost/this_is_a_test.htm
####
#!/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
}
}
}