fwbennett has asked for the wisdom of the Perl Monks concerning the following question:
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:
Is the 'if (defined...' not doing what i think it should be doing?Bad index while coercing array into hash at c:/temp/parseit.pl line (e +quivelant of 7 in this case).
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: How do i determine if an XML array element exists in a hash?
by roboticus (Chancellor) on Dec 05, 2011 at 15:28 UTC | |
Re: How do i determine if an XML array element exists in a hash?
by Jenda (Abbot) on Dec 05, 2011 at 21:34 UTC | |
by runrig (Abbot) on Dec 05, 2011 at 23:40 UTC | |
by fwbennett (Initiate) on Dec 27, 2011 at 19:36 UTC | |
by Jenda (Abbot) on Dec 27, 2011 at 20:43 UTC | |
Re: How do i determine if an XML array element exists in a hash?
by grantm (Parson) on Dec 05, 2011 at 23:04 UTC | |
Re: How do i determine if an XML array element exists in a hash?
by TJPride (Pilgrim) on Dec 05, 2011 at 19:33 UTC |