xml - Perl insert new element into array subarray -



xml - Perl insert new element into array subarray -

hello trying insert new element go

<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

<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 have follows have been reading references , de referencing 2 days , still cant it. trying impossible or can show me how.

thanks!

use strict; utilize xml::simple; utilize data::dumper; $xs = xml::simple->new( xmldecl => '<?xml version="1.0" encoding="utf-8"?>', forcearray => [ 'item' ], keyattr => { }, rootname => 'sg-distribution', ); $opt = $xs->xmlin(\*data); force @{ $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 array 4 element representing each explicit_group element. want identify 1 name attribute cdev. since they're not indexed name (but position), you'll need iterate on array find right element.

for $explicit_group (@{ $opt->{'sa-coller'}{'explicit-group'} }) { if ($explicit_group->{name} eq 'cdev') { force @{ $explicit_group->{'aip-address'} }, '99.99.99.254'; } }

xml arrays perl hash

Comments

Popular posts from this blog

web services - java.lang.NoClassDefFoundError: Could not initialize class net.sf.cglib.proxy.Enhancer -

Accessing MATLAB's unicode strings from C -

javascript - mongodb won't find my schema method in nested container -