Hi gus!!

I'm using XML::Parser to parse the following XML:
<level name="Exanet admin" value="1"> <menu cliname="NFS" clicmd="nfs"> <menu cliname="Exports" clicmd="exports"> <command clicmd="add" state="nfsExportsAdd"/> <command clicmd="edit" state="nfsExportsEdit"/> <command clicmd="view" state="nfsExportsView"/> </menu> <menu cliname="Configure" clicmd="configure"> <command clicmd="set" state="nfsConfigSet"/> <command clicmd="view" state="nfsConfigView"/> </menu> </menu> </level>
This XML holds a menus tree and available commands in each menu. I parse the file using the Objects style like that:
my $parser = new XML::Parser(Style => 'Objects'); my $xml = $parser->parsefile('test.xml');
I want to be able to print this menu to the user (on Unix, not to browser necessarily), therefore I have to iterate recursively on $xml returned from parsefile, something like:
&printXML($xml); sub printXML { my $ref = shift; if (ref($ref) eq 'HASH') { print "Type: HASH\n"; while (($key, $val) = each(%{$ref})) { print "key: $key, val: $val\n"; &printXML($val); } } elsif (ref($ref) eq 'ARRAY') { # kids print "Type: ARRAY\n"; for (@{$ref}) { print "bla: $_\n"; &printXML($_); } } elsif ($xml->isa('HASH')) { # elements print "Type: hash object\n"; while (($key, $val) = each(%{$ref})) { print "key: $key, val: $val\n"; &printXML($val); } } else { # text print $ref, "\n"; } }
from what I read, there can be only hash refs, array refs and hash objects inside $xml.
the thing is that I get the following error while executing the above: "Can't call method "isa" unblessed reference at on test.pl line 103".

Can anyone shed a little light? and maybe note about the above code

Hotshot

In reply to Parsing XML by hotshot

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.