in reply to help on how to get information from XML file using XML::Twig requested

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
  • Comment on Re: help on how to get information from XML file using XML::Twig requested
  • Download Code