I have an XML of the below format:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<outer_tag>
<inner_tag name="Name_0" template="Template_0">
<string name="Name_1"><default>DEFAULT_NAME</default></string>
<string name="Name_2"><default /></string>
<string name="Name_3"><default></default></string>
</inner_tag>
</outer_tag>
I need to modify the element with content "DEFAULT_NAME" , also, it's parent tag attribute needs to be "Name_1".
So, the output will be something like this:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<outer_tag>
<inner_tag name="Name_0" template="Template_0">
<string name="Name_1"><default>**NEW_NAME**</default></string>
<string name="Name_2"><default /></string>
<string name="Name_3"><default></default></string>
</inner_tag>
</outer_tag>
I tried the below code:
#!/usr/bin/perl
use strict;
use warnings;
use XML::Simple;
my $xml_file = 'xml1.xml';
my $xml = XMLin(
$xml_file,
KeepRoot => 1,
ForceArray => 1,
);
$xml->{outer_tag}->[0]->{string}->[0]->{default} = 'New_Name';
XMLout(
$xml,
KeepRoot => 1,
NoAttr => 1,
OutputFile => $xml_file,
);
But it's adding the New_Name1 at the end, not modifying the one I needed to replace. I'm new to XML in Perl.
->is optional between bracket subscripts, so you could use this instead:$xml->{outer_tag}[0]{string}[0]{default} = 'New_Name'