AhmedABdo has asked for the wisdom of the Perl Monks concerning the following question:

Hi all, I have this simple perl script working on an xml file, I want to print two attributes
and one element, I just got two attributes, the element "observed", is not printed
Could you help me to fix it please this is my code
Thanks in advance =========================
use XML::Twig; my $file = 'sample.xml'; my $twig = XML::Twig->new(); $twig->parsefile($file); my $root = $twig->root; foreach my $species ($root->descendants('Ss')) { print $species->att('ssId'); print "\t"; print $species->att('handle'); print "\t"; print $species->next_sibling_text('Observed'); print "\n"; }
======================== This is part of the file ======================
<?xml version="1.0" encoding="UTF-8"?> <ExchangeSet xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xml +ns="http:/ /www.ncbi.nlm.nih.gov/SNP/docsum" xsi:schemaLocation="http://www.ncbi. +nlm.nih.go v/SNP/docsum ftp://ftp.ncbi.nlm.nih.gov/snp/specs/docsum_3.4.xsd" spec +Version="3 .4" dbSnpBuild="144" generated="2015-05-26 09:54"> <SourceDatabase taxId="9606" organism="human" gpipeOrgAbbr="hs"/> <Rs rsId="3894" snpClass="snp" snpType="notwithdrawn" molType="gen +omic" genotype="true" bitField="050028000005130500030100" taxId="9606 +"> <Het type="est" value="0.05" stdError="0.1547"/> <Validation byCluster="true" byOtherPop="true" byHapMap="true" + by1000G=" true"> <otherPopBatchId>7179</otherPopBatchId> </Validation> <Create build="36" date="2000-09-19 17:02"/> <Update build="144" date="2015-05-07 10:52"/> <Sequence exemplarSs="491581208" ancestralAllele="C,C"> <Seq5>ATAAGCAAATAACTGAAGTTTAATCAGTCTCCTCCCAGCAAGTGATATGCAA +CTGAGATTCC TTATGACACATCTGAACACTAGTGGATTTGCTTTGTAGTAGGAACAAGGTACATTCGCGGGATAAATGTG +GCCAAGTTTT ATCTGCTGCCAGGGCTTTCAAATAGGTTGACCTGACAATGGGTCACCTCTGGGACTGA</Seq5> <Observed>C/T</Observed> <Seq3>AATTAGGAAGAGCTGGTACCTAAAATGAAAGATGCCCTTAAATTTCAGATTC +ACAATTTTTT TTTCTTAGTATAAGCATGTCCCATGTAATATCTGGGATATACTCATACCTTTAAAAATGTGCTCATTGTT +TATCTGAAAT TCACATTTTAACAGGGAACCATTGTTTTGTTATTGTTTATTGTTTTGTTTCTAAATAA</Seq3> </Sequence> <br> <Ss ssId="3931" handle="OEFNER" batchId="489" locSnpId="M +3" subSnpClass="snp" orient="forward" strand="bottom" molType="genomi +c" buildId="36" methodClass="DHPLC" validated="by-cluster"> <Sequence> <Seq5>TAATCAGTCTCCTCCCAGCAAGTGATATGCAACTGAGATTCCTTATGA +CACATCTGAACACTAGTGGATTTGCTTTGTAGTAGGAACAAGGTACATTCGCGGGATAAATGTGGCCAA +GTTTTATCTGCTGCCAGGGCTTTCAAATAGGTTGACCTGACAATGGGTCACCTCTGGGA CTGA</Seq5> <Observed>C/T</Observed> <Seq3>AATTAGGAAGAGCTGGTACCTAAAATGAAAGATGCCCTTAAATTTCAG +ATTCACAATTTT</Seq3>
</Sequence>

Replies are listed 'Best First'.
Re: Print XML elements and attributes
by Athanasius (Archbishop) on Aug 30, 2015 at 15:57 UTC

    Hello AhmedABdo, and welcome to the Monastery!

    Try changing:

    print $species->next_sibling_text('Observed');

    to:

    for my $observed ($species->descendants('Observed')) { print $observed->text(); }

    (Also, please add <code> ... </code> tags around the file data.)

    Hope that helps,

    Athanasius <°(((><contra mundum Iustus alius egestas vitae, eros Piratica,

      Thanks A lot, it helped me and fixed the problem. Also, thanks for reminding me for using