Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

add/delete/update records to a xml file, suitable Perl Module?

by mikeht (Initiate)
on Jul 30, 2008 at 02:31 UTC ( [id://700998]=perlquestion: print w/replies, xml ) Need Help??

mikeht has asked for the wisdom of the Perl Monks concerning the following question:

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.

Replies are listed 'Best First'.
Re: add/delete/update records to a xml file, suitable Perl Module?
by AltBlue (Chaplain) on Jul 30, 2008 at 03:35 UTC
    ... push @{ $xml->{server} }, $newsrv; $xml->{server}[-1]{address}->set_node(1);
Re: add/delete/update records to a xml file, suitable Perl Module?
by dHarry (Abbot) on Jul 30, 2008 at 12:29 UTC
Re: add/delete/update records to a xml file, suitable Perl Module?
by Bloodnok (Vicar) on Jul 30, 2008 at 11:03 UTC
    Why would you want to exclude the standard XML header ... seems a bit odd to me - but thanx anyway for the pointer to XML::Smart.

    At last, a user level that overstates my experience :-))
        Hi Jenda ,

        My reading of the OP was that XML tools were to be used for the manipulation of the XML files - if that weren't the case, then you have a point that I totally failed to spot.
        Otherwise, the header would be of no concern since the tool of choice would handle it.

        At last, a user level that overstates my experience :-))

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://700998]
Approved by pc88mxer
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others sharing their wisdom with the Monastery: (4)
As of 2025-11-14 08:23 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    What's your view on AI coding assistants?





    Results (70 votes). Check out past polls.

    Notices?
    hippoepoptai's answer Re: how do I set a cookie and redirect was blessed by hippo!
    erzuuliAnonymous Monks are no longer allowed to use Super Search, due to an excessive use of this resource by robots.