Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

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 ( [id://941924]=note: print w/replies, xml ) Need Help??


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

The thing is that there are (at least sometimes) several <asset>s in each <account> so the $account->{asset} is an array ref. The problem with XML::Simple is that it's not always an arrayref.

You may use this instead:

use strict; use XML::Rules; sub do_something_with { print join(', ', @_), "\n"; } my $parser = XML::Rules->new( rules => { _default => 'content', asset => 'as array no content', account => sub { my ($tag, $attr) = @_; foreach my $asset (@{$attr->{asset}}) { do_something_with($attr->{accountId}, $attr->{accountN +ame}, $asset->{assetId}, $asset->{assetName}); } }, } ); $parser->parse(\*DATA); __DATA__ <data> <account> <accountId>001</accountId> <accountName>Account 001</accountName> <asset> <assetId>abcd</assetId> <assetName>Asset abcd</assetName> </asset> ...

Or if you'd prefer a data structure containing all the data:

use strict; use XML::Rules; my $parser = XML::Rules->new( rules => { _default => 'content', 'asset,account' => 'as array no content', 'data' => 'pass no content' } ); use Data::Dumper; print Dumper($parser->parse(\*DATA));

Jenda
Enoch was right!
Enjoy the last years of Rome.

Replies are listed 'Best First'.
Re^2: How do i determine if an XML array element exists in a hash?
by runrig (Abbot) on Dec 05, 2011 at 23:40 UTC
    Or if you'd prefer a data structure containing all the data...
    Or even this might be preferable (using 'id' fields as hash keys):
    my $parser = XML::Rules->new( rules => { _default => 'content', 'asset' => 'no content by assetId', 'account' => 'no content by accountId', 'data' => 'pass no content' } );
    Update: Although looking at the OP, if there are really repeated accountId's, then maybe not...but I'm thinking that might be just due to cut n paste.
Re^2: How do i determine if an XML array element exists in a hash?
by fwbennett (Initiate) on Dec 27, 2011 at 19:36 UTC
    Hello all, This is Fantastic! However, there is one minor issue. With the sample xml above, if I return just one 'account', I get the error: "Bad index while coercing array into hash..." Jenda was correct - it is not always an array ref, and what you have above, handles 1 level - at the "asset" or 'sub' level. Would I set up a 'Rules' sub-routine inside of a 'Rules' sub-routine? Would that even work? Thank you again, -Fred

      Depends on how do you "return just one account". Show me the exact code that gives you the error.

      Jenda
      Enoch was right!
      Enjoy the last years of Rome.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://941924]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others perusing the Monastery: (5)
As of 2024-03-28 13:21 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found