gpshute has asked for the wisdom of the Perl Monks concerning the following question:

I am not very experienced with perl and hashes. I was looking for some help with displaying values from a SOAP request that returns XLM. I have used the XMLin function to parse the XML, but I don't understand the syntax to display individual elements, and perhaps to list them all. I can see the data that I want in the structure using dumper. (some output included below) Thanks for your help:
#!/usr/bin/perl #use strict; use LWP::UserAgent; use XML::Simple; use XML::Parser; use Data::Dumper; # this is the RPC call: $xml_file = '<?xml version="1.0" encoding="UTF-8"?> <SOAP-ENV:Envelope SOAP-ENV:encodingStyle="http://schemas.xmlsoap.or +g/soap/encoding/" xmlns:SOAP-ENC="http://schemas.xmlsoap.org/so +ap/encoding/" xmlns:xsi="http://www.w3.org/1999/XMLSchema-insta +nce" xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" x +mlns:xsd="http://www.w3.org/1999/XMLSchema" > <SOAP-ENV:Header xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oa +sis-200401-wss-wssecurity-secext-1.0.xsd"> <wsse:Security SOAP-ENC:root="1"> <wsse:UsernameToken> <wsse:Username xsi:type="xsd:string"><Removed></wsse:Username> <wsse:Password xsi:type="xsd:string"><Removed></wsse:Password> </wsse:UsernameToken> </wsse:Security> </SOAP-ENV:Header> <SOAP-ENV:Body> <ns1:retrieveTrafficStats xmlns:ns1="http://service.stats.ws.waas.cisc +o.com" SOAP-ENC:root="1"> <name><removed></name> <objType>wae</objType> <trafficType>optimized</trafficType> <direction>outbound</direction> <timeframe> <startTime xsi:type="xsd:string">2010-09-27T12:00:00</star +tTime> <endTime xsi:type="xsd:string">2010-09-27T14:00:00</endTime> <frequency xsi:type="xsd:string">lasthour</frequency> <timezone xsi:type="xsd:string">utc</timezone> </timeframe> </ns1:retrieveTrafficStats> </SOAP-ENV:Body> </SOAP-ENV:Envelope>'; my $server = '<removed>'; my $port = '8443'; my $RPCPath = '/ws/TrafficStats'; my $uri = 'https://' . $server . ':' . $port . $RPCPath; my $ua = LWP::UserAgent->new; # we need to authenticate the https connection: $ua->credentials( "", "TrafficStats", "admin", "default" ); $ua->ssl_opts("verify_hostname" =>"0"); my $header = HTTP::Headers->new; $header->header('SOAPAction' => '"retrieveTrafficStats"'); # this is to define what SOAP RPC call we are making my $request = HTTP::Request->new("POST",$uri,$header ); $request->content($xml_file); my $response = $ua->request($request); print $response->content; my $output = new XML::Simple(); #my $output = new XML::Parser(Style => 'Debug'); $xmlout = $response->content; $result=$output->XMLin($xmlout); print "result dumper\n"; print Dumper($result); print "result dumper\n";
-------------- dumper output: $VAR1 = {
'soapenv:Body' => {
'ns:retrieveTrafficStatsResponse' => {
'xmlns:ns' => '
http://service.stats.ws.waas.cisco.com',
'xmlns:ax27' =>
'http://stats.ws.waas.cisco.com/xsd',
'xmlns:ax24' =>
'http://rmi.java/xsd',
'xmlns:ax26' =>
'http://util.ws.waas.cisco.com/xsd',
'xmlns:ax25' =>
'http://io.java/xsd',
'ns:return' =>
[
{
'ax27:passthroughotherin' => '18831',
'ax27:frequency' => 'min',
'ax27:passthroughpolicyin' => '63309241',
'ax27:passthroughoverloadout' => '-1',
'type' => 'com.cisco.waas.ws.stats.TrafficStats',
'ax27:passthroughpolicyout' => '164244598',
'ax27:passthroughintermediateout' => '122180531',
'ax27:compressedin' => '391370667',
'ax27:deviceName' => '<removed>',
'ax27:applicationname' => 'All Traffic',
'ax27:endtime' => '2015-04-30T07:59:17.304Z',
'ax27:compressedout' => '864063076',
'ax27:passthroughintermediatein' => '24109685',
'ax27:uncompressedout' => '535554446',
'ax27:passthroughpeerin' => '0',
'ax27:passthroughpeerout' => '0',
'ax27:starttime' => '2015-04-30T07:54:17.256Z',
'ax27:uncompressedin' => '1413828417',
'ax27:passthroughotherout' => '49179',
'ax27:passthroughoverloadin' => '-1'
},
{
'type' => 'com.cisco.waas.ws.stats.TrafficStats',
'ax27:passthroughoverloadout' => '-1',
'ax27:passthroughpolicyin' => '71192344',
'ax27:passthroughotherin' => '22382',
'ax27:frequency' => 'min',
'ax27:deviceName' => '<removed>',
'ax27:passthroughpolicyout' => '169230011',
'ax27:passthroughintermediateout' => '126671655',
'ax27:compressedin' => '370427975',
'ax27:uncompressedout' => '544864065',
'ax27:compressedout' => '1130966047',
'ax27:endtime' => '2015-04-30T07:54:17.256Z',
'ax27:applicationname' => 'All Traffic',
'ax27:passthroughintermediatein' => '31383318',
'ax27:passthroughotherout' => '52600',
'ax27:passthroughoverloadin' => '-1',
'ax27:passthroughpeerin' => '0',
'ax27:passthroughpeerout' => '0',
'ax27:uncompressedin' => '1797753870',
'ax27:starttime' => '2015-04-30T07:49:17.209Z'
},

Replies are listed 'Best First'.
Re: Extracting Values from SOAP XML
by hippo (Archbishop) on Apr 30, 2015 at 08:38 UTC

    If you are having trouble accessing individual elements of a single, large, composite data structure then perhaps all you require is to have a quick read through perldsc? If after doing that you still have problems accessing a specific element, then by all means post some further details (and be sure to use <code> tags when you do - see Markup in the Monastery).

Re: Extracting Values from SOAP XML
by Anonymous Monk on Apr 30, 2015 at 08:38 UTC
Re: Extracting Values from SOAP XML
by locked_user sundialsvc4 (Abbot) on Apr 30, 2015 at 13:12 UTC

    The standard, “correct” (IMHO) way to locate and to extract values from an XML packet is through XPath expressions, which are kind of like a query-language for XML.   XML::LibXML supports this natively (using the libxml library, which is also industry-standard).   (There’s a very good chance that your data-source used that library to build the file you’re now consuming.)   You can simply ask for the nodes that you want, and get a list.   Your application does not have to contain “convoluted logic” to match the convoluted structure of the XML.   Some Perl SOAP-packages include this functionality.   Therefore, I would not use XML::Simple for this.

    There are many complete SOAP packages in CPAN.   Just choose and install one, and let it “drive you all the way to where you want to go.”