in reply to Re: Adding a hash to a hash reference.
in thread Adding a hash to a hash reference.
A slightly different version that keeps root node:
use strict; use XML::Simple; use Data::Dumper; my $xs = new XML::Simple("KeepRoot" => 1); my $xml = $xs->XMLin("policies.xml", forcearray => 1, keyattr => ['pol +icies']); my $new = { 'name' => "1", 'link_name' => "2", 'description' => "3", 'date' => "4" }; # Add to hash print Dumper($xml); $xml->{'policies'}->[0]->{'added'} = $new; print "Content-type: text/html\n\n"; # Sort hash #@{ $xml->{'policies'} } = sort { "\L$a->{name}" cmp "\L$b->{name}"} @ +{ $xml->{'policies'} }; print Dumper($xml); $xs->XMLout($xml, outputfile => "policies1.xml", attrindent => 1);
Output:
<policies> <New name="Test3" date="28-8-104" description="asdf" link_name="Test3.doc" /> <Newer name="Whatever" date="28-8-104" description="asdf" link_name="Whatever.doc" /> <added name="1" date="4" description="3" link_name="2" /> <policies2 name="General Admit to GPRMC" catagory="General" description="" link_name="General Admit to GPRMC.doc" /> </policies>
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^3: Adding a hash to a hash reference.
by bkiahg (Pilgrim) on Sep 29, 2004 at 23:13 UTC |