Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

Re: Re: Re: XML Twig parse

by Anonymous Monk
on Jan 21, 2003 at 14:21 UTC ( [id://228684]=note: print w/replies, xml ) Need Help??


in reply to Re: Re: XML Twig parse
in thread XML Twig parse

Thanks Mirod

One last question, what is the syntax to get the children of children?

I.E.

The children of rra and or their children cdp_prep or database
Thanks

Replies are listed 'Best First'.
Re: Re: Re: Re: XML Twig parse
by mirod (Canon) on Jan 21, 2003 at 15:30 UTC

    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>

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://228684]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others surveying the Monastery: (5)
As of 2024-03-29 10:36 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found