0

I'm using XML::Simple package to import an XML file and change a few properties of some of the child tags. The change is visible when the data is dumped with:

 print Dumper($data);

But how can I write this edited data into a new XML file? I did go through the CPAN page, but some code regarding this would really help.

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

2 Answers2

2
my $ref = XMLin(...);

# ...

open my $fh, ">", $path or die "$0: open $path: $!";
print $fh XMLout($ref);
close $fh or warn "$0: close $path: $!";
Greg Bacon
  • 134,834
  • 32
  • 188
  • 245
2

Use the XMLout method with the OutputFile option. Here is an example (names have been changed to protect the innocent :):

use strict;
use warnings;
use XML::Simple;

my $href = {
      'dir'        => '/tmp/foo/',
      'file'       => '/tmp/foo.debug',
      'abc'        => {
          'boo' => {
              'num'     => '55',
              'name'    => 'bill',
          },
          'goo' => {
              'num'     => '42',
              'name'    => 'mike',
          },
      }
};

my $xml = XMLout($href, OutputFile => 'out.xml');

__END__

The contents of the file 'out.xml' are:

<opt dir="/tmp/foo/" file="/tmp/foo.debug">
  <abc name="bill" num="55" />
  <abc name="mike" num="42" />
</opt>
toolic
  • 57,801
  • 17
  • 75
  • 117