Change your handler to look for 'feature' elements, and use xpaths to scope down to your 'residue' elements:
my $xfile = <<EOF; <sas_residue_annotation xmlns="http://url/Schema" xmlns:xsi="http://ww +w.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://url/Sche +ma test.xsd"> <features> <feature> <feature_name>feature_one</feature_name> <residues> <residue> <residue_index>2</residue_index> <residue_name>R</residue_name> </residue> <residue> <residue_index>4</residue_index> <residue_name>V</residue_name> </residue> </residues> </feature> <feature> <feature_name>feature_two</feature_name> <residues> <residue> <residue_index>5</residue_index> <residue_name>S</residue_name> </residue> </residues> </feature> </features> EOF use strict; use warnings; use XML::Twig; my $twig= new XML::Twig( twig_handlers => { feature => \&feature } ); $twig->parse($xfile); sub feature { my ($twig, $feat) = @_; if ($feat->first_child('feature_name')->text() eq 'feature_one') { for my $res ($feat->findnodes('residues/residue')) { my $res_idx = $res->first_child('residue_index')->text(); my $res_name = $res->first_child('residue_name' )->text(); print "$res_idx $res_name\n"; } } } __END__ 2 R 4 V

In reply to Re: help on how to get information from XML file using XML::Twig requested by toolic
in thread help on how to get information from XML file using XML::Twig requested by Angharad

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.