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.
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: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.