The following calls a user supplied routine with a list of ancestors of a node when it encounters a node with the right name in the XML tree.

use feature ":5.14"; use warnings FATAL => qw(all); use strict; use Data::Dump qw(dump pp); use XML::Simple; my $x = XMLin(<<'END'); <files name="Common.h"> <enums> <members kind="enum" name="PermissionLevel" protection="public" +static="no" virtualness="non_virtual"> <values initializer=" 99" name="PERMISSION_PUBLIC"></values> <values initializer=" 98" name="PERMISSION_NDA"></values> </members> <members kind="enum" name="RegisterType" protection="public" sta +tic="no" virtualness="non_virtual"> <values initializer=" 4" name="REGISTER_PCI"></values> </members> </enums> <functions> <members const="no" kind="function" name="Compare" protection="p +ublic" static="no" type="bool" virtualness="non_virtual" volatile="no +"> <parameters declaration_name="first" type="T"/> <parameters declaration_name="second" type="T"/> </members> </functions> </files> END pp($x); sub r($$$;$); sub r($$$;$) {my ($r, $l, $e, $a) = @_; $a = [] unless $a; return unless $l and ref($l); if (ref($l) =~ /HASH/) {for(sort keys %$l) {unless (/$e/) {push @$a, $_; r($r, $l->{$_}, $e, $a); pop @$a; } else {&$r(@$a); } } } elsif (ref($l) =~ /ARRAY/) {for(1..@$l) {unless ($l->[$_-1] =~ /$e/) {push @$a, $_; r($r, $l->[$_-1], $e, $a); pop @$a; } else {&$r(@$a); } } } } r sub {say "@_"}, $x, "values";

Produces

enums members PermissionLevel
enums members RegisterType

In reply to Re: getting ancestors of element by philiprbrenan
in thread getting ancestors of element by jccunning

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.