If you do not insist on using XML::TreeBuilder:

use strict; use XML::Rules; use Data::Dumper; my $parser = XML::Rules->new( stripspaces => 7, rules => { 'known-action,name' => 'content', 'target' => sub { return $_[1]->{partner} => $_[1]->{'known-action'}; }, 'targets' => 'no content', 'drug' => 'pass no content' } ); my $data = $parser->parse(\*DATA); #print Dumper($data); print "Name: $data->{name}\nType: $data->{type}\n"; while (my ($partner, $action) = each %{$data->{targets}}) { print " $partner: $action\n"; } __DATA__ <drug type="small molecule"> <name>Goserelin</name> <targets> <target partner="1"> <known-action>yes</known-action> </target> <target partner="2"> <known-action>yes</known-action> </target> <target partner="3"> <known-action>yes</known-action> </target> </targets> </drug>

Or if you insist on having a separate array of partners and actions:

use strict; use XML::Rules; use Data::Dumper; my $parser = XML::Rules->new( stripspaces => 7, rules => { 'known-action,name' => 'content', 'target' => sub { return '@partners' => $_[1]->{partner}, '@actions' => $_[1 +]->{'known-action'}; }, 'targets' => 'pass no content', 'drug' => 'pass no content' } ); my $data = $parser->parse(\*DATA); #print Dumper($data); print "Name: $data->{name}\nType: $data->{type}\n"; print "Partners: ", join(', ', @{$data->{partners}}), "\n"; print "Actions: ", join(', ', @{$data->{actions}}), "\n"; __DATA__ <drug type="small molecule"> <name>Goserelin</name> <targets> <target partner="1"> <known-action>yes</known-action> </target> <target partner="2"> <known-action>yes</known-action> </target> <target partner="3"> <known-action>yes</known-action> </target> </targets> </drug>

If the actual XML contains several <drug> tags, you should change its rule to "as array" or "by name" (uncomment the print Dumper($data); line to see what data structure you get in each case) or change it to a subroutine, that'll find the data in $_[1] in place of $data and may do whatever's necessary with that twig and forget the data that are no longer needed.

Jenda
Enoch was right!
Enjoy the last years of Rome.


In reply to Re: XML::TreeBuilder capture multiple attributes to array by Jenda
in thread XML::TreeBuilder capture multiple attributes to array by yesitsjess

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.