dereference xml :: simple output是一个复杂的数据结构,它是一个哈希数组的哈希值

时间:2013-10-23 19:53:30

标签: perl

我正在尝试使用xml :: simple解析xml .Here是xml :: simple的输出

        $VAR1 = {
 'soapenv:Body'=>[
              {
               'ns1:queryResponse'=>[
                                     {
                                      'ns1:result'=>[
                                                     {
                                                      'ns1:done'=>['true'],
                                                                     'ns1:queryLocator' => [
                                                                                           {
                                                                                             'xsi:nil' => '1',
                                                                                             'xmlns:xsi' => 'http://www.w3.org/2001/XMLSchema-instance'
                                                                                           }
                                                                                         ],
                                                                     'ns1:size' => [
                                                                                   '60'
                                                                                 ],
                                                                     'ns1:records' => [
                                                                                      {
                                                                                        'ns2:RefundTransactionTime' => [
                                                                                                                       '2013-09-12T13:17:18.000-08:00'
                                                                                                                     ],
                                                                                        'xmlns:ns2' => 'http://object.abccom/',
                                                                                        'ns2:MethodType' => [
                                                                                                            'CreditCard'
                                                                                                          ],
                                                                                        'ns2:Gateway' => [
                                                                                                         'Chase Paymentech'
                                                                                                       ],
                                                                                        'ns2:Type' => [
                                                                                                      'Electronic'
                                                                                                    ],
                                                                                        'ns2:RefundDate' => [
                                                                                                            '2013-09-12T00:00:00'
                                                                                                          ],
                                                                                        'xmlns:xsi' => 'http://www.w3.org/2001/XMLSchema-instance',
                                                                                        'ns2:Status' => [
                                                                                                        'Processed'
                                                                                                      ],
                                                                                        'ns2:Id' => [
                                                                                                    '2c92c0f8410f4d9a014113d2170a2e17'
                                                                                                  ],
                                                                                        'xsi:type' => 'ns2:Refund',
                                                                                        'ns2:AccountId' => [
                                                                                                           '2c92c0f9410f55ee0141132b6c936e15'
                                                                                                         ],
                                                                                        'ns2:Amount' => [
                                                                                                        '99'
                                                                                                      ],
                                                                                        'ns2:CreatedDate' => [
                                                                                                             '2013-09-12T13:17:17.000-08:00'
                                                                                                           ]
                                                                                      },
                                                                                      {
                                                                                        'xmlns:ns2' => 'http://object.abccom/',
                                                                                        'ns2:MethodType' => [
                                                                                                            'CreditCard'
                                                                                                          ],
                                                                                        'ns2:Type' => [
                                                                                                      'External'
                                                                                                    ],
                                                                                        'ns2:RefundDate' => [
                                                                                                            '2013-10-12T00:00:00'
                                                                                                          ],
                                                                                        'xmlns:xsi' => 'http://www.w3.org/2001/XMLSchema-instance',
                                                                                        'ns2:Status' => [
                                                                                                        'Processed'
                                                                                                      ],
                                                                                        'ns2:Id' => [
                                                                                                    '2c92c0f8410f4d9a0141145bfbb61a9b'
                                                                                                  ],
                                                                                        'xsi:type' => 'ns2:Refund',
                                                                                        'ns2:AccountId' => [
                                                                                                           '2c92c0f8410f4d8701411411a9ad79b7'
                                                                                                         ],
                                                                                        'ns2:Amount' => [
                                                                                                        '477.74'
                                                                                                      ],
                                                                                        'ns2:CreatedDate' => [
                                                                                                             '2013-09-12T15:47:54.000-08:00'
                                                                                                           ],
                                                                                        'ns2:Comment' => [
                                                                                                         '16 Payment Exception - Chargeback'
                                                                                                       ]
                                                                                      }
                                                                                    ]
                                                                   }
                                                                 ],
                                                   'xmlns:ns1' => 'http://api.abccom/'
                                                 }
                                               ]
                        }
                      ],
      'xmlns:soapenv' => 'http://schemas.xmlsoap.org/soap/envelope/'
    };

我正在使用以下代码:

#!/usr/bin/env perl
use strict;
use Data::Dumper;
use XML::Simple qw(:strict);

my $data = XMLin('Refund.xml', forcearray=>1, keyattr=>[] );
print "Reference type in data is : ", ref($data), "\n";

print Dumper($data);

#Try to access the values
my $records=$data->{"soapenv:Body"}->[0]->{"ns1:queryResponse"}->[0]->{"ns1:result"}-> 
[0]->{"ns1:records"};

foreach my $record ( @{ $records } ) {
    print $record->{"ns2:RefundTransactionTime"};
    print "\n";
 }

print Dumper($ data)生成包含哈希数组的哈希引用。

我想将上面生成的哈希引用格式化为数组引用格式数组,如下所示:

 [
   [
    "AccountId",
    "Id",
    "Amount",
    "CreatedDate",
   "Comment",
    "Status",
   "Type",
  "RefundDate",
  "MethodType"
  ],
[
 "2c92c0f8410f4d8701411411a9ad79b7",
 "2c92c0f8410f4d9a0141145bfbb61a9b",
  "477.74",
  "2013-09-12T15:47:54.000-08:00",
  "16 Payment Exception - Chargeback",
  "Processes",
  "External",
 "2013-10-12T00:00:00",
 "CreditCard"
],
[
 "2c92c0f9410f55ee0141132b6c936e15",
 "2c92c0f8410f4d9a014113d2170a2e17",
 "99",
 "2013-09-12T13:17:17.000-08:00",
 "",
 "Processed",
 "Electronic",
 "2013-09-12T00:00:00",
 "Chase Paymentech"
 ]
],

非常感谢任何帮助。谢谢

1 个答案:

答案 0 :(得分:1)

返回值ARRAY(0x1f9fca98)表示$record->{"ns2:RefundTransactionTime"}包含数组引用。您可能想要第一个元素:$record->{"ns2:RefundTransactionTime"}->[0]