2012-03-22 6 views
0

私は、XML :: Simpleを使ってPerlを使用して、ハッシュをXMLドキュメントに変換しています。XML ::単純なXMLノードが 'name'ノードの値に変換されました

次のように私のスクリプトが見えます:

#!/usr/bin/perl 
use strict; 
use warnings; 
use XML::Simple; 
use Data::Dumper; 

my $xml_simple = XML::Simple->new(NoAttr => 1, 
        KeepRoot => 1); 

my $hash = { output => { 'products' => [ { 'product' => { 'titel' => 'ABN AMRO Bank hypotheken', 
                  'owner' => 'ABN AMRO Hypotheken Groep', 
                  'code' => 'ABN AMRO BANK R' } }, 
             { 'product' => { 'titel' => 'Aegon', 
                  'owner' => 'AEGON Hypotheken', 
                  'code' => 'AEGON pilot' } } ], 
         'date' => '2012-02-20'} }; 


my $xml = $xml_simple->XMLout($hash); 

print Dumper($xml); 

私は取得しています出力は次のようになります。

<output> 
    <date>2012-02-20</date> 
    <products> 
     <name>product</name> 
     <code>ABN AMRO BANK R</code> 
     <owner>ABN AMRO Hypotheken Groep</owner> 
     <titel>ABN AMRO Bank hypotheken</titel> 
    </products> 
    <products> 
     <name>product</name> 
     <code>AEGON pilot</code> 
     <owner>AEGON Hypotheken</owner> 
     <titel>Aegon</titel> 
    </products> 
</output> 

しかし、私が探していますが、これは( '製品' ノードを参照してください)です。

<output> 
    <date>2012-02-20</date> 
    <products> 
    <product> 
     <code>ABN AMRO BANK R</code> 
     <owner>ABN AMRO Hypotheken Groep</owner> 
     <titel>ABN AMRO Bank hypotheken</titel> 
    </product> 
    <product> 
     <code>AEGON pilot</code> 
     <owner>AEGON Hypotheken</owner> 
     <titel>Aegon</titel> 
    </product> 
    </products> 
</output> 

これはXML :: Simpleを使って行うことができますか、別のモジュールを使用する必要がありますか?

答えて

3

あなたは、XML :: Simpleは、それが

#!/usr/bin/perl 
use strict; 
use warnings; 
use XML::Simple; 
use Data::Dumper; 
my $desired_xml = << 'END'; 
<myxml> 
<output> 
    <date>2012-02-20</date> 
    <products> 
    <product> 
     <code>ABN AMRO BANK R</code> 
     <owner>ABN AMRO Hypotheken Groep</owner> 
     <titel>ABN AMRO Bank hypotheken</titel> 
    </product> 
    <product> 
     <code>AEGON pilot</code> 
     <owner>AEGON Hypotheken</owner> 
     <titel>Aegon</titel> 
    </product> 
    </products> 
</output> 
</myxml> 
END 
#print $desired_xml; 
my $xml_simple = XML::Simple->new(
    NoAttr => 1, 
    KeepRoot => 1 
); 
#my $hash = XMLin($desired_xml, forcearray => 1); 
my $hash = { 
    output => [ 
     { 
      date  => ["2012-02-20"], 
      products => [ 
       { 
        product => [ 
         { 
          code => ["ABN AMRO BANK R"], 
          owner => ["ABN AMRO Hypotheken Groep"], 
          titel => ["ABN AMRO Bank hypotheken"], 
         }, 
         { 
          code => ["AEGON pilot"], 
          owner => ["AEGON Hypotheken"], 
          titel => ["Aegon"], 
         }, 
        ], 
       }, 
      ], 
     }, 
    ], 
    }; 
#print Data::Dumper->Dump([$hash], [qw(hash)]); 
my $xml = $xml_simple->XMLout($hash); 
print Data::Dumper->Dump([$xml], [qw(xml)]); 
+1

+1を望んでどのようなデータ構造を教えて聞かせすることができます。明示的にコード内の1つのことを説明する:XMLoutにForceArrayを設定する必要はありません。 – daxim

+0

ありがとうございました。私は実際にあなたの答えの直前で私のハッシュの構造を演奏することによってそれを稼働させました。自分のやり方で(つまり、XMLで必要なことを伝えるだけで)、苦痛を軽減します。 –

+0

パラメータ 'KeyAttr => {}'を使用すると、配列の折り畳みを完全に無効にすると、XMLの再構成が中止されます。 – Borodin

関連する問題