【问题标题】:XML::Simple, XML nodes turned into value of 'name' nodesXML::Simple,XML 节点变成了 'name' 节点的值
【发布时间】:2012-03-22 12:25:02
【问题描述】:

我正在使用 Perl 和 XML::Simple 将散列转换为 XML 文档。

我的脚本如下所示:

#!/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 可行还是我应该使用不同的模块?

【问题讨论】:

    标签: perl xml-simple


    【解决方案1】:

    你可以让 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。在代码中明确说明一件事:XMLout 不需要设置 ForceArray。
    • 谢谢菲利普。实际上,在您回答之前,我通过玩弄我的哈希结构使其正常工作。以你的方式去做(即让 XML 简单地告诉你它需要什么)不会那么痛苦。
    • 完全禁用数组折叠,使用参数 KeyAttr =&gt; {} 将停止 XML 的重组。
    猜你喜欢
    • 2012-12-03
    • 2018-08-27
    • 1970-01-01
    • 1970-01-01
    • 2013-03-19
    • 2018-08-24
    • 2014-08-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多