Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical

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);

Replies are listed 'Best First'.
Re: Error parsing XML
by GrandFather (Sage) 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";



    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?

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

How do I use this? | Other CB clients
Other Users?
Others lurking in the Monastery: (7)
As of 2019-11-17 18:12 GMT
Find Nodes?
    Voting Booth?
    Strict and warnings: which comes first?

    Results (86 votes). Check out past polls.