Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

Comment on

( #3333=superdoc: print w/ replies, xml ) Need Help??

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:

if ($checks->{Detail}) { foreach my $detail (@{$checks->{Detail}->{Row}}) { foreach my $col (@{$detail->{Col}}) { print "$col \n"; } } }
And here is a piece of the data:
<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?


In reply to parsing complex xml structure with xml::simple by bfdi533

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!
  • 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, 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, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • Outside of code tags, you may need to use entities for some characters:
            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.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others having an uproarious good time at the Monastery: (8)
    As of 2014-07-11 00:42 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      When choosing user names for websites, I prefer to use:








      Results (217 votes), past polls