views:

64

answers:

2

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.

+1  A: 
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
+2  A: 

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