http://www.perlmonks.org?node_id=1170247


in reply to Parsing with XML::Simple

You said that you want the value -2225 which is in the MaxCode element but you're looking for it in the ErrorCode element.

Replies are listed 'Best First'.
Re^2: Parsing with XML::Simple
by Anonymous Monk on Aug 23, 2016 at 19:05 UTC
    I just typed the sample code wrong, sorry!
    #!/usr/bin/perl use strict; use warnings; use XML::Simple; use Data::Dumper; use Data::Dump 'pp'; #my $content = $response->decoded_content(); my $content = qq|<?xml version=\'1.0\' encoding=\'UTF-8\'?><soap:Envel +ope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"> <soap:Header><ais:requestID xmlns:ais="http://com"> {7f000001-47db2420000001569783cdb88136} </ais:requestID> </soap:Header> <soap:Body> <soap:Fault> <faultcode>soap:Server</faultcode> <faultstring> check expression: </faultstring> <faultactor>CheckTwo</faultactor> <detail> <longDescription xmlns="http://com"> Exception occurred </longDescription> <MaxCode xmlns="http://com/Server">-2225</MaxCode> </detail> </soap:Fault> </soap:Body> </soap:Envelope>|; #my $parser = new XML::Simple; my $parser= XML::Simple->new( KeepRoot => 1, KeyAttr => [], ForceArray + => [qw( MaxCode )] ); my $data = $parser->XMLin($content); my $result = $data->{'soap:Body'}->{'soap:Fault'}->{'detail'}->{'MaxCo +de'};

    Thanks!

      Okay. I ran this code and noticed that you probably want to start at the top.

      my $result = $data->{'soap:Envelope'}{'soap:Body'}{'soap:Fault'}{detail}{MaxCode}; # removed unnecessary punctuation

      This won't give you the answer you seek but push you in the right direction. I'll also give you a hint... You told it to treat MaxCode as an array.

        Got to work by simplifying the code like this, with one issue:
        ... my $parser = new XML::Simple; my $data = $parser->XMLin($content); my $result = $data->{'soap:Body'}{'soap:Fault'}{'detail'}{'MaxCode'}; print $result->{content};

        The only thing is that once the value of "$result" is empty, I am getting this:
        Use of uninitialized value in string