bfdi533 has asked for the wisdom of the Perl Monks concerning the following question:
I have an xml input file that I need to parse and the structure is pretty complex. I have been working my way through the structure fairly well but have run into a situation that I do not know how to handle. I am using code to dereference parts of the xml structure to more easily deal with them.
One part is throwing me though. I have a set that is a Row(s) of Detail info that can either contain a hash or an array of hashes. I had been dealing with this as an array of hashes but when it gets to a single hash rather than an array the code crashes with an "Not an ARRAY reference at ..." error.
Here is the code:
And here is a piece of the data:if ($checks->{Detail}) { foreach my $detail (@{$checks->{Detail}->{Row}}) { foreach my $col (@{$detail->{Col}}) { print "$col \n"; } } }
<Check> <Detail> <Head> <Col>User</Col> <Col>Weak Password</Col> <Col>Locked Out</Col> <Col>Disabled</Col> </Head> <Row Grade="0"> <Col>Guest</Col> <Col>Weak</Col> <Col>-</Col> <Col>Disabled</Col> </Row> <Row Grade="0"> <Col>SUPPORT_388945a0</Col> <Col>-</Col> <Col>-</Col> <Col>Disabled</Col> </Row> <Row Grade="5"> <Col>Administrator</Col> <Col>-</Col> <Col>-</Col> <Col>-</Col> </Row> </Detail> </Check> <Check> <Detail> <Head> <Col>Drive Letter</Col> <Col>File System</Col> </Head> <Row Grade="5"> <Col>C:</Col> <Col>NTFS</Col> </Row> </Detail> </Check>
How can I go about handling the multiple rows versus the single row situation with efficient code?
Is there a way to check to see if a variable is an array a has and execute an if/then/else code segment?
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: parsing complex xml structure with xml::simple
by davorg (Chancellor) on Aug 01, 2006 at 14:25 UTC | |
Re: parsing complex xml structure with xml::simple
by ptum (Priest) on Aug 01, 2006 at 14:26 UTC | |
by bfdi533 (Friar) on Aug 01, 2006 at 15:06 UTC | |
Re: parsing complex xml structure with xml::simple
by GrandFather (Saint) on Aug 01, 2006 at 18:50 UTC | |
Re: parsing complex xml structure with xml::simple
by planetscape (Chancellor) on Aug 02, 2006 at 18:04 UTC |