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

Peeps, I'm a bit stumped by a piece of code that doesn't behave like I think it should. I'm simply trying to parse an XML document with XML::Simple and reference one of the fields. The code is:-
# parse the message (executed within eval so parsing errors are no +n-fatal) eval { $parsed_xml = XMLin( $message, forcearray => 1, keyattr => [] ); }; # if parsing errors were encountered, write the appropriate messag +e to the log if ($@) { &stop_it($ERROR, $XMLERROR, "Parsing error encountered - $@", +$log_file); } $queue_name = $parsed_xml->{descriptor}->{inputQueue}; &log_it($INFO, "inputQueue is $queue_name", $log_file); print Dumper($parsed_xml);
When I check the log, $queue_name is blank. When I view the Data::Dumper output I see:-
'descriptor' => [ { 'uniqueId' => [ 'X\'414d51204d534f4c5530 +3220202020203ed6d4ec00023332\'' ], 'resubCounter' => [ '0' ], 'failureTimestamp' => [ '2004-01-13 15:0 +0:00.000' ], 'errorCode' => [ '1234' ], 'messageType' => [ 'TEST.MESSAGE' ], 'inputQueue' => [ 'BLACKHOLE' ], 'destinationSystem' => [ 'NOWHERE' ], 'dataFormat' => [ 'XML' ], 'portfolioCode' => [ 'NONE' ], 'errorDescription' => [ 'Blackhole messa +ge' ], 'sourceSystem' => [ 'TEST' ] } ], 'body' => [ { 'contents' => [ 'Some data' ] } ]
So, it looks to me like the field descriptor/inputQueue is present but not being picked up by the code. I've had code like this working in the past but for the life of me I can't spot what I'm doing wrong. Can you? Thanks, J.

Replies are listed 'Best First'.
Re: XML parsing problem.
by Art_XIV (Hermit) on Jan 13, 2004 at 18:06 UTC

    You forgot to consider the nesting:

    $queue_name = $parsed_xml->{'descriptor'}[0]{'inputQueue'}[0]

    Hanlon's Razor - "Never attribute to malice that which can be adequately explained by stupidity"
Re: XML parsing problem.
by gmpassos (Priest) on Jan 14, 2004 at 01:37 UTC
    This is not a XML::Simple problem, since XML::Simple did the job right, it has created the HASH tree of the XML for you.

    Your error is in the access of the HASH tree! Hehehe, and this remeber me why I have created XML::Smart, where the both access to the tree are right:

    $parsed_xml->{descriptor}->[0]->{inputQueue}; # or $parsed_xml->{descriptor}{inputQueue};

    Graciliano M. P.
    "Creativity is the expression of the liberty".

      Thanks, guys. I knew it was a piece of blatant stupidity on my part.
Re: XML parsing problem.
by derby (Abbot) on Jan 13, 2004 at 18:02 UTC
    forcearray => 1 so:

    $parsed_xml->{descriptor}->[0]->{inputQueue}; # or $parsed_xml->{descriptor}[0]{inputQueue};

    -derby

    update: Doh! As Art_XIV points out below:

    $parsed_xml->{descriptor}[0]{inputQueue}[0];