There is no level2_children method, but you might be able to use descendants, or just use a map. Note that you can use a regexp (which will be applied to the element name) as a condition. I am sure there are other ways as well, the condition can be a code ref for example, so you could test exactly what you want in there.

Anyway, here is some example code:

#!/usr/bin/perl -lw use strict; use XML::Twig; my $t= XML::Twig->new( pretty_print => 'indented', error_context => 1) ->parse( \*DATA); my $rra= $t->root; { print "results in document order:"; my @results= map { $_, $_->children( qr/cdp_prep|database/) } $rra-> +children; print $_->id foreach (@results); } { print "results by level then document order:"; my @results= $rra->children; push @results, map { $_->children( qr/^(cdp_prep|database)$/) } @res +ults; print $_->id foreach (@results); } { # works only if cdp_prep and database elements can only be found as # grand-children of rra print "results using descendants:"; print $_->id foreach ( $rra->children, $rra->descendants( qr/^(cdp_prep|database)$/) ); } __DATA__ <rra> <child id="child_01"> <cdp_prep id="cdp_prep_01">content</cdp_prep> <database id="database_01">content</database> <other id="other_01">content</other> <cdp_prep id="cdp_prep_02"> <other id="other_02">content</other> </cdp_prep> </child> <child id="child_02"> <other id="other_03">content</other> <database id="database_02">content</database> </child> <child id="child_03"> <other id="other_04"><other id="other_05">content</other></other> </child> </rra>

In reply to Re: Re: Re: Re: XML Twig parse by mirod
in thread XML Twig parse by Anonymous Monk

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.