XML :: Simple&访问复杂的结构

时间:2012-05-02 16:30:16

标签: xml arrays perl hash xml-parsing

我在使用Perl / XML :: Simple访问XML响应中的一些嵌套数据时遇到问题。打印的XML回复的摘录如下所示:

<?xml version="1.0" encoding="UTF-8"?>
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<soapenv:Body>
<ns1:SelectCmDeviceResponse soapenv:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" xmlns:ns1="http://schemas.cisco.com/ast/soap/">
<SelectCmDeviceResult xsi:type="ns1:SelectCmDeviceResult">
<TotalDevicesFound xsi:type="xsd:unsignedInt">3</TotalDevicesFound>
<CmNodes soapenc:arrayType="ns1:CmNode[3]" xsi:type="soapenc:Array" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/">
<item xsi:type="ns1:CmNode">
<ReturnCode xsi:type="ns1:RisReturnCode">NotFound</ReturnCode>
<Name xsi:type="xsd:string">10.186.78.4</Name>
<NoChange xsi:type="xsd:boolean">false</NoChange>
<CmDevices soapenc:arrayType="ns1:CmDevice[0]" xsi:type="soapenc:Array"/>
</item>
<item xsi:type="ns1:CmNode">
<ReturnCode xsi:type="ns1:RisReturnCode">Ok</ReturnCode>
<Name xsi:type="xsd:string">10.186.78.68</Name>
<NoChange xsi:type="xsd:boolean">false</NoChange>
<CmDevices soapenc:arrayType="ns1:CmDevice[2]" xsi:type="soapenc:Array">
<item xsi:type="ns1:CmDevice">
<Name xsi:type="xsd:string">SEPD0574CF73FC0</Name>
<IpAddress xsi:type="xsd:string">10.186.79.41</IpAddress>
<DirNumber xsi:type="xsd:string">51251001-Registered,51251004-Registered,51251002-Registered</DirNumber>
<Class xsi:type="ns1:DeviceClass">Phone</Class>
<Model xsi:type="xsd:unsignedInt">404</Model>
<Product xsi:type="xsd:unsignedInt">303</Product>
<BoxProduct xsi:type="xsd:unsignedInt">0</BoxProduct>

这是代码,它应该解析响应并返回返回设备的IpAddress值:

#!/usr/bin/perl
use strict;
use warnings;
use Data::Dumper;
use XML::Simple;
use LWP;
use SOAP::Lite;

my $cucmip = "10.1.10.1";
my $axl_port = "8443";
my $user = "admin";
my $password = "password";
my $axltoolkit = "http://schemas.cisco.com/ast/soap/action/#RisPort#SelectCmDevice";

sub getDevIp {
    my $message = "<?xml POST message>

    my $url="https://$cucmip:$axl_port/realtimeservice/services/RisPort?wsdl";
    my $ua = LWP::UserAgent->new;
    my $header = new HTTP::Headers (
    'Content-Type' => 'application/xml; charset=utf-8',
    'SOAPAction' => 'http://schemas.cisco.com/ast/soap/action/#RisPort#SelectCmDevice',
    );
    my $req = HTTP::Request->new(POST => $url, $header, $message);
    $req->authorization_basic($user,$password);
    my $response = $ua->request($req);
    my $xs = new XML::Simple(KeyAttr=>[]);
    my $data = $xs->XMLin($response->content);
    print $data->{'soapenv:Body'}->{'ns1:SelectCmDeviceResponse'}->{'SelectCmDeviceResult'}->{'CmNodes'}->{'item'}->[0]->{'CmDevices'}->{'item'}->[0]->{'IpAddress'}->{'content'};
}

getDevIp();

2 个答案:

答案 0 :(得分:3)

这基本上就是你所拥有的。

$VAR1 = {
    'soapenv:Body' => {
        'ns1:SelectCmDeviceResponse' => {
            'SelectCmDeviceResult' => {
                'CmNodes' => {
                    'item' => [
                        {
                            'xsi:type'  => 'ns1:CmNode',
                            'CmDevices' => {
                                'soapenc:arrayType' => 'ns1:CmDevice[0]',
                                'xsi:type'          => 'soapenc:Array'
                            },
                        },
                    ],
                    # plus some more items with DIFFERENT structure
                },
            },
        },
    },
};

您正尝试使用

进行访问
$data->{'soapenv:Body'}
    ->{'ns1:SelectCmDeviceResponse'}
        ->{'SelectCmDeviceResult'}
            ->{'CmNodes'}
                ->{'item'}->[0]                # you probably want ->[1] here! (wild guess)
                    ->{'CmDevices'}
                        ->{'item'}->[0]        # this data does not exist
                            ->{'IpAddress'}    # and this does not exist
                                ->{'content'}; # and this

不存在的值是由perl在首次访问时创建的(这称为autovivification)并初始化为undef

这就是你警告的原因。

答案 1 :(得分:1)

您可以尝试SOAP::Deserializer。我无法尝试,因为我无法访问您正在解析的XML文档。它返回一个SOAP::SOM对象。

  

valueof(node)

    $res = $som->valueof('[1]');
  

SOAP::SOM对象在内部使用匹配方法匹配路径时,此方法允许检索任何匹配节点内的数据。数据以本机Perl数据的形式返回,而不是类实例(请参阅dataof)。在标量上下文中,此方法仅返回匹配节点集中的第一个元素。在数组列表上下文中,返回所有元素。假设先前调用发生在先前的匹配调用之后,它从$ som中包含的方法响应中检索结果实体,因为这是方法响应标记中的第一个子元素。