0

クエリ要素名を指定して、目的のノードのハッシュ {key, value} ペアを取得しようとしています。

次の perl コードは、次のように印刷できる出力を得ようとする試みです。

p $error_hash->{'errorCode'}
0
p $error_hash->{'errorMsg'}
"get device list successfully"

不足しているロジックの修正にご協力いただきありがとうございます。

ここに示すコードは、「値」のみを出力します。を使用する場合でも$xml_hash、目的のハッシュを取得する前に、最初の 3 つのノードを手動でプルーニングする必要があります。

ありがとう、それを探しています。

#!/usr/bin/env perl
use strict;
use warnings;
use XML::LibXML;
use XML::Hash;

my $xml = do {local $/;  < DATA > };
my $parser = XML::LibXML->new();
my $doc    = $parser->parse_string($xml);

my $xpath="//getDeviceResponse";

my $xml_converter = XML::Hash->new();
my $xml_hash = $xml_converter->fromXMLStringtoHash($xml);

my (%errmsg) = "";
my (%devinfo) = "";

sub get_value {
    my $elem = $_[0];

    my $query="$xpath";
    foreach my $errorMsg ($doc->findnodes($query)) {
        my ($title) = $errorMsg->findnodes($elem);
        print $title->to_literal;
    }
}

get_value "errorMsg";    
get_value "deviceInfo";

__DATA__
<SOAP-ENV:Envelope
        xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"
        xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xmlns:xsd="http://www.w3.org/2001/XMLSchema"
        xmlns:ns1="http://jaxb.dev.java.net/array"
        xmlns:ns3="http://r200806.ws.abc.com/">
        <SOAP-ENV:Body>
                <getDeviceResponse>
                        <errorMsg>
                                <errorCode>0</errorCode>
                                <errorMsg>get device list successfully&#xA;</errorMsg>
                        </errorMsg>
                        <deviceInfo>
                                <devId>139</devId>
                                <firmware>abc</firmware>
                                <firmwareVersion>5.0</firmwareVersion>
                                <buildNum>208</buildNum>
                                <description></description>
                                <hostname>puppet</hostname>
                                <platform>puppetlabs</platform>
                                <sn>abc1234</sn>
                                <ip>172.168.210.2</ip>
                        </deviceInfo>
                </getDeviceResponse>
        </SOAP-ENV:Body>
</SOAP-ENV:Envelope>
4

2 に答える 2

0

これらの行は、私のシステムで警告を生成します:

my (%errmsg) = "";
my (%devinfo) = "";

あなたは xpath よりもハッシュに慣れているようです。そのため、それらを使用した例を示します。

my $getDeviceResponse_hash = $xml_hash->{'SOAP-ENV:Envelope'}{'SOAP-ENV:Body'}{'getDeviceResponse'};
my $errorMsg_hash = $getDeviceResponse_hash->{'errorMsg'};
print $errorMsg_hash->{'errorMsg'}{'text'};
print $errorMsg_hash->{'errorCode'}{'text'};

これらのハッシュの構造は、次のようにいつでも確認できます。

use Data::Dumper;
...
print Dumper($errorMsg_hash);
于 2013-09-12T22:26:56.940 に答える