Hello I am trying to insert a new element so that I go from this
<explicit-group name="CDEV">
<aip-address>1.1.1.1</aip-address>
<aip-address>2.2.2.2</aip-address>
<aip-address>3.3.3.3</aip-address>
To this
<explicit-group name="CDEV">
<aip-address>1.1.1.1</aip-address>
<aip-address>2.2.2.2</aip-address>
<aip-address>3.3.3.3</aip-address>
<aip-address>99.99.99.254</aip-address>
The code I have is as follows but I have been reading about references and de referencing for two days and still cant get it. Am I trying to do something impossible or can someone show me how.
Thanks!
use strict;
use XML::Simple;
use Data::Dumper;
my $xs = XML::Simple->new(
XMLDecl => '<?xml version="1.0" encoding="UTF-8"?>',
forcearray => [ 'item' ],
keyattr => { },
rootname => 'SG-distribution',
);
my $opt = $xs->XMLin(\*DATA);
push @{ $opt->{'sa-coller'}->{'explicit-group'} } , { {'CDEV'}->{'aip-address'} };
print Dumper($opt);
print $xs->XMLout($opt);
__DATA__
<?xml version="1.0" encoding="UTF-8"?><SG-distribution>
<sa-coller name="W8-C1" enabled="true" host="localhost" port="99">
<ip-group name="home" ipAddressMask="192.168.0.*" match="glob"/>
<ip-group name="home2" ipAddressMask="10.0.0.*" match="glob"/>
<explicit-group name="CDEV">
<aip-address>1.1.1.1</aip-address>
<aip-address>2.2.2.2</aip-address>
<aip-address>3.3.3.3</aip-address>
</explicit-group>
<explicit-group name="HYU"/>
<explicit-group name="JUN"/>
</sa-coller>
</SG-distribution>
You have an array with four element representing each
explicit_groupelement. You want to identify the one whose name attribute isCDEV. Since they’re not indexed by name (but by position), so you’ll need to iterate over the array to find the right element.