I am having a very long xml and I wish to update the attribute value of one of the tag which is very deep nested so don't want to go node by node. Also structure is not same for the intended node always as can be seen below: Input XML is:
<Re>
<Co Class="Parameter" ID="CSCP001" Status="Available">
<FileSpec URL="c://mine/testfiles/wln/c.txt"/>
<CoOp Operation="Tag" SourceCS="RGB" SourceObjects="All">
<FileSpec Resource="SourceProfile" URL="c://mine/testfiles/wln/d.txt"/>
</CoOp>
</Co>
<Ru Class="Parameter" ID="IDR002" PartIDKeys="Run" Status="Available">
<Ru EndOfDocument="true" Pages="0" Run="1" RunTag="First">
<La>
<FileSpec URL="c://mine/testfiles/wln/e.txt"/>
</La>
</Ru>
</Ru>
</Re>
and I wish to have output xml as
<Re>
<Co Class="Parameter" ID="CSCP001" Status="Available">
<FileSpec URL="d://yours/wln/c.txt"/>
<CoOp Operation="Tag" SourceCS="RGB" SourceObjects="All">
<FileSpec Resource="SourceProfile" URL="d://yours/wln/d.txt"/>
</CoOp>
</Co>
<Ru Class="Parameter" ID="IDR002" PartIDKeys="Run" Status="Available">
<Ru EndOfDocument="true" Pages="0" Run="1" RunTag="First">
<La>
<FileSpec URL="d://yours/wln/e.txt"/>
</La>
</Ru>
</Ru>
</Re>
I tried using xml simple, xmllib but not able to do the required. I am new in perl programming.
use XML::LibXML qw( );
use XML::LibXML;
use Data::Dumper;
my $xml = "a.txt";
my $xpath_expression = 'FileSpec';
my $parser = XML::LibXML->new();
my $doc = $parser->parse_file($xml) or warn "Could not";
my $parser1 = XML::LibXML::Element->new($xml);
for my $FileSpec1 ($doc->getElementsByTagName('FileSpec'))
{
print $FileSpec1;
my $xpath = '$FileSpec1/@URL';
my ($attr) = $doc->findnodes($xpath);
$attr->setValue('dfdsa');
my ($URL1) = $FileSpec1->findvalue('@URL');
print $URL1;
}
I tried using $node->setAttribute( $aname, $avalue ); but this is throwing exceptions. Please advice.