After discussion here with you guys in my last thread, I've been teaching myself to get information from a xml document using XML::Twig. A portion of the xml file is here
<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>
So far, I've managed to print out the 'residue_index' and 'residue_name' info from within the 'residue' tag. But I need to expand upon that and only print this information for 'feature_one' and not for 'feature_two'- at the moment its printing the information regardless of what feature 'residue_name' and 'residue_index' is in.

Any advice as to how I may achieve this much appreciated.

Code thus far:

#!/usr/bin/perl # use module use Data::Dumper; use strict; use warnings; use XML::Twig; my $file = shift; my $twig= new XML::Twig( twig_handlers => { residue => \&residue } ); $twig->parsefile($file); sub residue { my ($twig, $res) = @_; my $res_idx = $res->first_child('residue_index')->text; my $res_name = $res->first_child('residue_name')->text; print "$res_idx $res_name\n"; }

In reply to 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.