2012-03-22 2 views
0

XML을 사용하여 Perl을 사용하고 있습니다. Simple을 사용하여 해시를 XML 문서로 변환합니다.XML :: Simple, 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로이 행할 수 있습니까 :: 단순하거나 다른 모듈을 사용해야합니까?

답변

3

당신은 XML :: 간단한 데이터 구조가

#!/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

일을 원하는 것을 말할하도록 할 수 있습니다. 명시 적으로 코드에서 한 가지만 설명하려면 XMLout에 ForceArray를 설정하지 않아도됩니다. – daxim

+0

감사합니다. Philip. 실제로 해시 구조로 놀아서 대답하기 바로 전에 실제로 작동하게했습니다. 자신의 방식대로 (예 : XML로 간단하게 필요한 것을 알려줌으로써) 고통을 덜 덜 수 있습니다. –

+0

'KeyAttr => {}'매개 변수를 사용하여 배열 접기를 사용 불가능하게하면 XML의 재구성이 중지됩니다. – Borodin

관련 문제