in reply to Re: XML::TreeBuilder capture multiple attributes to array
in thread XML::TreeBuilder capture multiple attributes to array

The issue is I asked a colleague for advise whilst waiting for a reply from that thread and as a result my most complete script is all written to use TreeBuilder now. I would have the same problem if I used Twig I feel. I just need it to return 1, 2, 3 etc. After writing 5 scripts, doing some serious googling and pestering colleagues via email I still can't get it to work. Google results show examples where people have used the above example or:

@array = $drug->find_by_tag_name('groups')->find_by_tag_name('group')->as_text;

But I can't get this to capture all the information I need. Getting very fed up and disheartened so whoever helps me out should know in advance they're doing their super good deed of the day!

Replies are listed 'Best First'.
Re^3: XML::TreeBuilder capture multiple attributes to array
by toolic (Bishop) on Jul 05, 2012 at 14:20 UTC
    You could do your "super good deed of the day" by posting:
    • Legal XML code: you have no closing drug tag and mismatched target/targets tags.
    • Perl code which shows how you construct your TreeBuilder object and call the parse method.
    By posting an incomplete question you are likely to get incomplete answers.

      OOPS sorry again! That XML still incomplete, this is correct now:

      <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> </target> </drug>

      Here's the complete Perl script, which just got deleted when I updated:

      use XML::TreeBuilder; my $file = $ARGV[0]; my $tree = XML::TreeBuilder->new(); $tree->parse_file($file); foreach my $drug ($tree->find_by_tag_name('drug')){ my @targets = $drug->look_down ('_tag', 'targets')->look_down ('_tag', + 'target')->attr_get_i('partner'); foreach my $id (@targets){print "\t$id";} my @actions = $drug->look_down ('_tag', 'targets')->look_down ('_tag', + 'known-action')->as_text; foreach my $act (@actions){print "\t$act";} }

      And also because I updated I think all my apologising is gone, so just to summarise: I'm sorry for how that came across, I was just trying to let people know that I really do appreciate the help a lot

        Broken XML again :|

        Also broken, XML::TreeBuilder/XML::Element/attr_get_i

        This works

        #!/usr/bin/perl -- use strict; use warnings; use Data::Dump; use XML::TreeBuilder; my $tree = XML::TreeBuilder->new(); $tree->parse(<<'__XML__'); <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> __XML__ foreach my $drug ( $tree->find_by_tag_name('drug') ) { my @targets = $drug->look_down( '_tag', 'target' ); for my $target ( @targets ){ print $target->attr('partner'), "\t"; } print "\n"; my @act = $drug->look_down( '_tag', 'known-action' ); @act = map { $_->as_trimmed_text } @act; print join "\t", @act, "\n"; }