Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

Error parsing XML

by Hammy (Scribe)
on Mar 01, 2006 at 03:58 UTC ( #533574=perlquestion: print w/ replies, xml ) Need Help??
Hammy has asked for the wisdom of the Perl Monks concerning the following question:

Hello again, I have an XML stream that I am trying to process and I can not figure out how. The problem I am running into is sometimes one of the nodes is coming back with multiple nodes in an array and sometimes it is not. How do I check what the format is before continuing. It is either going to be:
$NRE_ID = $XMLref->{'diffgr:diffgram'}->{'NewDataSet'}->{'Table'}->[0] +->{'CLIENT_ID'}; or $NRE_ID = $XMLref->{'diffgr:diffgram'}->{'NewDataSet'}->{'Table'}->{'C +LIENT_ID'};
WHen I use one or the other I get an error. I'd love to say if blah do else do. This seems like a really simple issue, but I can not figure it out from documentation. Building the XML is as follows:
$XMLref = XMLin($returnStr, suppressempty => ''); $xmlstream = Dumper($XMLref);
Thanks.

Comment on Error parsing XML
Select or Download Code
Re: Error parsing XML
by GrandFather (Cardinal) on Mar 01, 2006 at 04:19 UTC

    You can use ref to detect the type of thing you have:

    use strict; use warnings; my $array = []; my $hash = {}; print ref $array, "\n"; print ref $hash, "\n";

    Prints:

    ARRAY HASH

    DWIM is Perl's answer to Gödel
      Thank you ever so much, that did the trick.
      You can use ref to detect the type of thing you have

      Yes you can, but in this case it would be better to use XML::Simple's ForceArray option to ensure that the 'Table' elements are always represented as an array. That way you use the same code to process them regardless of whether there's one or many.

      More info here.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://533574]
Approved by GrandFather
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others wandering the Monastery: (10)
As of 2014-09-22 13:17 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (192 votes), past polls