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>
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.
|