It may not seem like it, but this is actually an XY problem. "XML::Simple" is dirty bad and wrong. Try using XML::Twig instead. (I think as you said Activeperl, XML::LibXML is unavailable, which would be another good choice generally, and comes bundled with Strawberry perl)

If you're trying to just print every node:

#!/usr/bin/env perl use strict; use warnings; use Data::Dumper; use XML::Twig; my $twig = XML::Twig -> parsefile ( 'data/misc/test.xml' ); foreach my $node ( $twig -> get_xpath ( '//*' ) ) { print $node -> tag, ": ", $node -> text,"\n"; }

Of course, this is a bit of an artificial scenario. You can do something similar to what you've got using "children" and "first_child" instead. But the real question is - what are you trying to accomplish?

Because XML::Simple is probably not the right tool to be using - it has very few redeeming features. Despite the name, it's not simple to use, it can only cope with simple XML

That's why it's officially discouraged, according to the module docs

For the sake of an exact comparison with your code:

print $twig -> root -> first_child_text('name'),"\n"; print $twig -> root -> first_child('profcats') -> first_child_text('na +me'),"\n"; foreach my $element ( $twig -> root -> children ) { print $element -> tag, ": ", $element -> text,"\n"; foreach my $sub ( $element -> children ) { print "\t",$sub -> tag, ": ", $sub -> text,"\n"; foreach my $subsub ( $sub -> children ) { print "\t\t",$subsub -> tag, ": ", $sub -> text,"\n"; foreach my $subsubsub ( $subsub -> children ) { print "\t\t\t", $subsubsub -> tag, ": ", $subsubsub -> +text,"\n"; } } } }

But honestly - I don't think you really need to do that, and one of the alternatives would be better

And whilst we're at it - turn on use strict; and use warnings;


In reply to Re: Walking Hash of hash issue? by Preceptor
in thread Walking Hash of hash issue? by Syrkres

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.