Perl code for Find and replace a tag value in XML

civaKadyKeype

New Member
Below is the XML I will be using:\[code\]<a>\[/code\]\[code\]<id>ABC</id>\[/code\]\[code\]<class />\[/code\]\[code\]<gender />\[/code\]\[code\]</a>\[/code\]I want to write a Perl code which search for tag 'id' and replace the value "ABC" with "DEF".However the nesting of above XML can change. So I want to make a generalized code that search for the tag 'id' independently of its exact position. Till now i am able to get the logic where i can replace the value in ABC but that makes my code static of the position of tag 'id'.\[code\]#!usr/bin/perluse warnings;use XML::Simple;use Spreadsheet::ParseExcel;use Data::Dumper;my $FileName = 'sample.xls';my $xml_file = 'hello.xml';$par=$ARGV[0];my $xml = XMLin($xml_file, KeepRoot=>1, ForceArray=>1,);$xml->{a}->[0]->{id}='DEF';XMLout( $xml, KeepRoot =>1 , NoAttr =>1, OutputFile => $xml_file, ); }\[/code\]
 
Back
Top