views:

115

answers:

3

I'm using XML::Simple to edit an XML file. After which the updated data is sent to a new XML file. But this procedure produces <opt></opt> tag to be added and the original parent tag is lost. I want to replace <opt> with the original tag name. How do I do that?

+1  A: 

See KeepRoot. You should also consider enabling strict mode.

#!/usr/bin/perl

use strict; use warnings;

use XML::Simple qw(:strict);
use Data::Dumper;

my $x = XMLin(\*DATA, KeepRoot => 1, ForceArray => 1, KeyAttr => ['the']);

print XMLout($x, KeepRoot => 1, KeyAttr => ['the']);

__DATA__
<this>
<that the="other">This that and the other</that>
</this>

Output:

<this>
  <that the="other">This that and the other</that>
</this>
Sinan Ünür
The <opt> tag has now encapsulates the original root node.Is there a way to complete remove the <opt> tag?
fixxxer
A: 

in the new xml file you can use regular experessions to find the pattern you want to remove and then replace with the pattern you want,that is original tag.

@ar="xml file"; $pat="tag you want to replace"; $rep="original tag";

foreach $a (@ar) { if ($a =~ s|$pat|$rep|gi; }

xml file hander name=@arr;

Prashant
+2  A: 

You're stretching the limits of XML::Simple. When you get to the point where you don't like exactly what it does, it's time for something else. What that something else is depends on your problem.

brian d foy
Thanks for the heads-up!
fixxxer