2010-01-13 91 views
0

我使用XML::Simple包來導入XML文件並更改一些子標籤的一些屬性。當數據被傾倒時,這個改變是可見的:如何將XML數據保存到XML :: Simple文件中?

print Dumper($data); 

但是我怎樣才能把這個編輯後的數據寫入一個新的XML文件呢?我確實經歷了CPAN頁面,但有些關於此的代碼確實有幫助。

回答

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

# ... 

open my $fh, ">", $path or die "$0: open $path: $!"; 
print $fh XMLout($ref); 
close $fh or warn "$0: close $path: $!"; 
2

使用XMLout方法和OutputFile選項。這裏是一個例子(名稱已被改變,以保護無辜:):

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>