2

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?

Sinan Ünür
  • 116,958
  • 15
  • 196
  • 339
fixxxer
  • 15,568
  • 15
  • 58
  • 76

3 Answers3

2

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
  • 116,958
  • 15
  • 196
  • 339
0

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;

brian d foy
  • 129,424
  • 31
  • 207
  • 592
Prashant
  • 47
  • 1
  • 4
0

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, but I like XML::Twig.

brian d foy
  • 129,424
  • 31
  • 207
  • 592
  • You can get these sorts of things done with XML::Simple, but my advice stands: unless it does exactly what you want without you having to do anything, move on to something else. – brian d foy Apr 24 '12 at 15:19