For start I've searched this site and used google, i've found that XML::Smart (latest from CPAN) to be closest in what can be used to add record(s) to a xml file. Other Perl Modules which could do the 3 functions welcome.
With XML::Smart:
Is there a way to avoid outputting the following 2 lines in filename.xml when using $XML->save('filename.xml')
<?xml version="1.0" encoding="iso-8859-1" ?>
<?meta name="GENERATOR" content="XML::Smart/1.6.9 Perl/5.008008 [cygwi
+n]" ?>
When running the examples in documentation:
#!/usr/bin/perl
use XML::Parser;
use XML::Smart;
use Data::Dumper;
my $XML = new XML::Smart(q`
<hosts>
<server os="linux" type="redhat" version="8.0">
<address>192.168.0.1</address>
<address>192.168.0.2</address>
</server>
<server os="linux" type="suse" version="7.0">
<address>192.168.1.10</address>
<address>192.168.1.20</address>
</server>
</hosts>
`,'smart');
$XML = $XML->cut_root ;
## Add a new server node:
my $newsrv = {
os => 'Linux' ,
type => 'Mandrake' ,
version => 8.9 ,
address => [qw(192.168.3.201)]
# address => [qw(192.168.3.201 192.168.3.202)]
} ;
push(@{$XML->{server}} , $newsrv) ;
$XML->save('newfile.xml') ;
Display is as follows:
<?xml version="1.0" encoding="iso-8859-1" ?>
<?meta name="GENERATOR" content="XML::Smart/1.6.9 Perl/5.008008 [cygwi
+n]" ?>
<hosts>
<server os="linux" type="redhat" version="8.0">
<address>192.168.0.1</address>
<address>192.168.0.2</address>
</server>
<server os="linux" type="suse" version="7.0">
<address>192.168.1.10</address>
<address>192.168.1.20</address>
</server>
<server address="192.168.3.201" os="Linux" type="Mandrake" version="
+8.9"/>
</hosts>
How to get it as (so that the items are inserted same order as in $newsvr object and not sorted). Wisdoms welcome
<hosts>
<server os="linux" type="redhat" version="8.0">
<address>192.168.0.1</address>
<address>192.168.0.2</address>
</server>
<server os="linux" type="suse" version="7.0">
<address>192.168.1.10</address>
<address>192.168.1.20</address>
</server>
<server os="Linux" type="Mandrake" version="8.9">
<address>192.168.3.201</address>
</server>
</hosts>
Thank you.