1
<?xml version="1.0"?>
<root>
<Arguments>

<apkName>Player
    <testUseCase>PlayVideo</testUseCase>
    <id>1</id>
    <clipName>firstclip</clipName>
</apkName>
</Arguments>

</root>

I tried this code: but its not working and its keeping player name in new tag with name with content and order also changing ..

use XML::Simple;

my $xml_file = "test.xml";

my $xml = XMLin(
    $xml_file,  
    KeepRoot => 1,
    ForceArray => 1,
)

$xml->{root}->[0]->{Arguments}->[0]->{apkName}->[0]->{clipName}->[0] = 'secondclip';
XMLout(
    $xml,
    XMLDecl =>1,
    KeepRoot => 1   ,
    NoAttr => 1,
    OutputFile => $xml_file,
);
Shashi
  • 15
  • 3

1 Answers1

1

Don't use XML::Simple:

#!/usr/bin/env perl
use strict;
use warnings;

use XML::Twig;

#parse your XML.
my $twig = XML::Twig -> new -> parsefile ( 'your_file.xml' );
#search for, and modify 'clipName' nodes containing the text 'firstclip'. 
$_ -> set_text('secondclip') for $twig -> findnodes('//clipName[string()="firstclip"]');

$twig -> set_pretty_print('indented');
$twig -> print;

Although are you sure apkName actually looks like that? It seem odd that the 'close' tag would be where it is.

To rewrite your existing file - XML::Twig has a parsefile_inplace mechanism, but I'd suggest it's overly complicated for what you're trying to do, and instead you just want to

open ( my $output, '>', 'output.new.xml' ) or die $!;
print {$output} $twig -> sprint;
Community
  • 1
  • 1
Sobrique
  • 52,974
  • 7
  • 60
  • 101