【问题标题】:Perl parse unusual XML into an array of hashesPerl 将异常 XML 解析为哈希数组
【发布时间】:2014-04-27 22:00:23
【问题描述】:

我正在尝试将 XML 解析为一个哈希数组,我知道您可以使用 XML::Simple 执行此操作,但它没有给我想要的结果。

这是我的 XML:

<?xml version="1.0" ?>
<resultset>
<table name="PROFILE">
 <column name="ID" type="String"/>
 <column name="VERSION" type="String"/>
 <column name="NAME" type="String"/>
 <column name="DESCRIPTION" type="String"/>
<data>
<r><c>0</c><c>1.0</c><c>Default profile</c><c>Default profile</c></r>
<r><c>2</c><c>1.2</c><c>Custom 2</c><c></c></r>
<r><c>3</c><c>6.0</c><c>Custom 3</c><c></c></r>
<r><c>1</c><c>1.15</c><c> For Compare</c><c>The built in profile for compare.</c></r>
<r><c>4</c><c>1.3</c><c>Custom 4</c><c> </c></r>
<r><c>6</c><c>11.0</c><c>Custom 6</c><c>Please only make approved changes.</c></r>
</data>
</table>
</resultset>

这是我使用XML::SimpleXMLin() 然后使用Data::Dumper 打印得到的输出:

$VAR1 = {
          'table' => {
                     'name' => 'PROFILE',
                     'data' => {
                               'r' => [
                                      {
                                        'c' => [
                                               '0',
                                               '1.0',
                                               'Default profile',
                                               'Default profile'
                                             ]
                                      },
                                      {
                                        'c' => [
                                               '2',
                                               '1.2',
                                               'Custom 2',
                                               {}
                                             ]
                                      },
                                      {
                                        'c' => [
                                               '3',
                                               '6.0',
                                               'Custom 3',
                                               {}
                                             ]
                                      },
                                      {
                                        'c' => [
                                               '1',
                                               '1.15',
                                               ' For Compare',
                                               'The built in profile for compare.'
                                             ]
                                      },
                                      {
                                        'c' => [
                                               '4',
                                               '1.3',
                                               'Custom 4',
                                               {}
                                             ]
                                      },
                                      {
                                        'c' => [
                                               '6',
                                               '11.0',
                                               'Custom 6',
                                               'Please only make approved changes.'
                                             ]
                                      }
                                    ]
                             },
                     'column' => {
                                 'ID' => {
                                         'type' => 'String'
                                       },
                                 'NAME' => {
                                           'type' => 'String'
                                         },
                                 'DESCRIPTION' => {
                                                  'type' => 'String'
                                                },
                                 'VERSION' => {
                                              'type' => 'String'
                                            }
                               }
                   }
        };

但我想要以下输出:

$VAR1 = [
          {
            'ID' => '0',
            'NAME' => 'Default profile',
            'DESCRIPTION' => 'Default profile',
            'VERSION' => '1.0'
          },
          {
            'ID' => '2',
            'NAME' => 'Custom 2',
            'DESCRIPTION' => '',
            'VERSION' => '1.2'
          },
          {
            'ID' => '3',
            'NAME' => 'Custom 3',
            'DESCRIPTION' => '',
            'VERSION' => '6.0'
          },
          {
            'ID' => '1',
            'NAME' => ' For Compare',
            'DESCRIPTION' => 'The built in profile for compare.',
            'VERSION' => '1.15'
          },
          {
            'ID' => '4',
            'NAME' => 'Custom 4',
            'DESCRIPTION' => ' ',
            'VERSION' => '1.3'
          },
          {
            'ID' => '6',
            'NAME' => 'Custom 6',
            'DESCRIPTION' => 'Please only make approved changes.',
            'VERSION' => '11.0'
          }
        ];

我不知道是否可以使用XML::Simple 获得此输出,或者这是否是最好的方法,但我查看了 CPAN 文档并在线搜索,但我不知道从哪里开始。有人可以帮我弄清楚如何获得所需的输出吗?

【问题讨论】:

    标签: xml perl xml-parsing xml-simple


    【解决方案1】:

    是的,为了让输出更接近你想要的,你可以再修改一下 XML::Simple 的设置。但是,最终您将需要进行某种翻译才能获得确切的目标。

    为什么不直接用你得到的东西做翻译呢?可能可以从 XML 中提取列名,并处理被转换为哈希引用的奇怪空数据,但我把它留给你:

    use XML::Simple;
    
    use strict;
    use warnings;
    
    my $data = do { local $/; <DATA> };
    
    my $xml = XMLin($data);
    
    my @formatted = map {
        {
            ID          => $_->{c}[0],
            VERSION     => $_->{c}[1],
            NAME        => $_->{c}[2],
            DESCRIPTION => ref $_->{c}[3] ? '' : $_->{c}[3],
        }
    } @{$xml->{table}{data}{r}};
    
    use Data::Dump;
    dd \@formatted;
    
    
    __DATA__
    <?xml version="1.0" ?>
    <resultset>
    <table name="PROFILE">
     <column name="ID" type="String"/>
     <column name="VERSION" type="String"/>
     <column name="NAME" type="String"/>
     <column name="DESCRIPTION" type="String"/>
    <data>
    <r><c>0</c><c>1.0</c><c>Default profile</c><c>Default profile</c></r>
    <r><c>2</c><c>1.2</c><c>Custom 2</c><c></c></r>
    <r><c>3</c><c>6.0</c><c>Custom 3</c><c></c></r>
    <r><c>1</c><c>1.15</c><c> For Compare</c><c>The built in profile for compare.</c></r>
    <r><c>4</c><c>1.3</c><c>Custom 4</c><c> </c></r>
    <r><c>6</c><c>11.0</c><c>Custom 6</c><c>Please only make approved changes.</c></r>
    </data>
    </table>
    </resultset>
    

    输出

    [
      {
        DESCRIPTION => "Default profile",
        ID => 0,
        NAME => "Default profile",
        VERSION => "1.0",
      },
      { DESCRIPTION => "", ID => 2, NAME => "Custom 2", VERSION => "1.2" },
      { DESCRIPTION => "", ID => 3, NAME => "Custom 3", VERSION => "6.0" },
      {
        DESCRIPTION => "The built in profile for compare.",
        ID => 1,
        NAME => " For Compare",
        VERSION => "1.15",
      },
      { DESCRIPTION => "", ID => 4, NAME => "Custom 4", VERSION => "1.3" },
      {
        DESCRIPTION => "Please only make approved changes.",
        ID => 6,
        NAME => "Custom 6",
        VERSION => "11.0",
      },
    ]
    

    【讨论】:

      猜你喜欢
      • 2016-07-25
      • 2013-01-06
      • 2021-02-27
      • 2010-12-26
      • 1970-01-01
      • 1970-01-01
      • 2013-03-06
      • 2021-07-13
      • 2013-02-04
      相关资源
      最近更新 更多