0
我正在使用Perl :: XML :: Simple将哈希转换为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 ::简单的或我应该使用不同的模块?
+1。为了明确地说明代码中的一件事:设置ForceArray对于XMLout不是必需的。 – daxim 2012-03-22 13:26:28
感谢Philip。我实际上是在你回答之前通过使用我的散列结构来工作的。按照你的方式(即让XML简单地告诉你它需要什么)就不那么痛苦了。 – 2012-03-22 13:28:14
完全禁用数组折叠,使用参数'KeyAttr => {}'将停止重构XML。 – Borodin 2012-03-22 14:00:50