Hello perl Monks, I need to check to see if an xml hash element exists so i can create an array to post to variables for "stuff".
It sounds complex, but really its not. I'm sure i'm over-looking something simple.
Here is the gist of it:

I have xml returned to me (using SOAP::Lite) that looks something like this:

<xml version, encoding & SOAP Envelope header stuff...> <account> <accountId>001</accountId> <accountName>Account 001</accountName> <asset> <assetId>abcd</assetId> <assetName>Asset abcd</assetName> </asset> <asset> <assetId>wxyz</assetId> <assetName>Asset wxyz</assetName> </asset> </account> <account> <accountId>001</accountId> <accountName>Account 001</accountName> </account> </SOAP Envelope stuff...>

As you can see from the xml response, I am being returned account information that may have many or no "assets".
I am trying to perform actions (do stuff) with each 'account' + 'asset' sets.

Ultimately, I need to create an array like this:

for each (asset that exists) { $array = (accountId, accountname, assetId, assetName); (do stuff with the array); }

note - the 'accountId' and 'accountName' need to be carried through to each relevant 'asset'.

my script seems to die when it hits the 'account' with no 'asset'.

my $data1 = $xml->XMLin($xmlResponse); foreach my $account (@{$data1->{'account'}}) { my $acctName = $account->{'accountName'}; my $acctId = $account->{'accountId'}; if (defined $account->{'ns1:asset'}) { our $assetID = $account->{'asset'}->{'assetId'}; our $assetName = $account->{'asset'}->{'assetName'}; } else { print "\nIt didn't process\n"; } }

The out put looks like this:

Bad index while coercing array into hash at c:/temp/parseit.pl line (e +quivelant of 7 in this case).
Is the 'if (defined...' not doing what i think it should be doing?

Thanks


In reply to How do i determine if an XML array element exists in a hash? by fwbennett

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.