【问题标题】:dereference xml::simple output which is complex data structure that is a hash of an array of hashs取消引用 xml::simple 输出,它是复杂的数据结构,是散列数组的散列
【发布时间】:2013-10-23 19:53:30
【问题描述】:

我正在尝试使用 xml::simple 解析 xml。这是 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"
 ]
],

非常感谢任何帮助。谢谢

【问题讨论】:

  • 到目前为止您尝试过什么?请阅读FAQHow to Ask,了解编写好的 SO 问题的技巧。提示:您应该在发帖前自己做出努力。
  • 吉姆,我正在尝试从上面代码中粘贴的 ns1:records 中检索一个值(ns2:RefundTransactionTime)。它返回我这个值:ARRAY(0x1f9fca98)而不是实际值。
  • 让它$record->{"ns2:RefundTransactionTime"}->[0]
  • 谢谢奴隶。我现在可以获取确切的值了。
  • @SlavenRezic 你的评论(加上解释)应该是一个答案。

标签: perl


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 2019-05-25
    • 1970-01-01
    • 2013-05-09
    • 2013-02-05
    • 2013-12-30
    • 1970-01-01
    • 2016-09-08
    • 1970-01-01
    相关资源
    最近更新 更多