Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

Error parsing XML

by Hammy (Scribe)
on Mar 01, 2006 at 03:58 UTC ( [id://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.

Replies are listed 'Best First'.
Re: Error parsing XML
by GrandFather (Saint) 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
Domain Nodelet?
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?Last hourOther CB clients
Other Users?
Others taking refuge in the Monastery: (6)
As of 2024-04-19 06:25 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found