How to retrieve node with same name in XML simple in perl -


i have xml file , want compare ids entry node id reaction node , if same example below want access information of reaction (substrate id , product id). have 2 product id , code gives first 1 here xml file

<?xml version="1.0"?> <!doctype pathway system "http://www.kegg.jp/kegg/xml/kgml_v0.7.1_.dtd"> <!-- creation date: may 31, 2012 14:53:24 +0900 (gmt+09:00) --> <pathway name="path:ko00010" org="ko" number="00010" >     <entry id="13">     </entry>     <entry id="37" >     </entry>     <reaction id="13" name="rn:r01070" type="reversible">       <substrate id="105" name="cpd:c05378"/>       <product id="132" name="cpd:c00118"/>       <product id="89" name="cpd:c00111"/>     </reaction> </pathway> 

here code

use strict; use warnings; use xml::simple;  $xml = new xml::simple; $data = $xml->xmlin("file.xml"); foreach $entry (keys %{$data->{entry}}) {     foreach $reaction (keys %{$data->{reaction}}) {     if ($data->{reaction}->{id} eq $data->{entry}->{$entry}->{id} ){         print "substrate:::$data->{reaction}->{substrate}->{id}\n";         print "product:::$data->{reaction}->{product}->{id}\n";     }     } } 

xml::simple simple. own documentation discourages further use of module.

the data structure might getting (who knows?) on system:

{   entry    => { 13 => {}, 37 => {} },   name     => "path:ko00010",   number   => "00010",   org      => "ko",   reaction => {                 id => 13,                 name => "rn:r01070",                 product => { "cpd:c00111" => { id => 89 }, "cpd:c00118" => { id => 132 } },                 substrate => { id => 105, name => "cpd:c05378" },                 type => "reversible",               }, } 

it inpect data structure when not sure if accessing correctly. 1 way use data::dumper; print dumper $data.

you might notice there no field id in entry. also, products not have id field, rather using name attribute name. *sigh* – kind of “cleverness” why shouldn't using xml::simple.


it far easier use proper parser xml::libxml. can use xpath select nodes want:

use xml::libxml; use feature 'say';  $data = xml::libxml->load_xml(location => "test.xml"); $query = '/pathway/reaction[/pathway/entry/@id=@id]';  if (my ($reaction) = $data->findnodes($query)) {   "substrate:::", $reaction->findvalue('substrate/@id');   "product:::", $_->textcontent $reaction->findnodes('product/@id'); } 

output:

substrate:::105 product:::132 product:::89 

Comments

Popular posts from this blog

How to remove text and logo OR add Overflow on Android ActionBar using AppCompat on API 8? -

html - How to style widget with post count different than without post count -

url rewriting - How to redirect a http POST with urlrewritefilter -