[英]How to retrieve node with same name in XML simple in perl
我有一个xml文件,我想比较入口节点的ID和反应节点的ID,如果与下面的示例相同,我想访问所有反应信息(底物ID和产品ID)。 我有两个产品ID,此代码给出了第一个。这是XML文件
<?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>
这是我的代码
use strict;
use warnings;
use XML::Simple;
my $xml = new XML::Simple;
my $data = $xml->XMLin("file.xml");
foreach my $entry (keys %{$data->{entry}}) {
foreach my $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绝非简单。 它自己的文档不鼓励进一步使用该模块。
您可能正在获取的数据结构(谁知道?)在我的系统上:
{
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",
},
}
当您不确定是否正确访问数据结构时,最好检查一下数据结构。 一种方法是use Data::Dumper; print Dumper $data
use Data::Dumper; print Dumper $data
。
您可能会注意到entry
没有id
字段。 而且, product
没有ID字段,而是使用name
属性作为名称。 *叹* –这种“聪明”是为什么您不应该使用XML :: Simple的原因。
使用适当的解析器(如XML::LibXML
)要容易得多。 然后,我们可以使用XPath选择所需的节点:
use XML::LibXML;
use feature 'say';
my $data = XML::LibXML->load_xml(location => "test.xml");
my $query = '/pathway/reaction[/pathway/entry/@id=@id]';
if (my ($reaction) = $data->findnodes($query)) {
say "substrate:::", $reaction->findvalue('substrate/@id');
say "product:::", $_->textContent for $reaction->findnodes('product/@id');
}
输出:
substrate:::105
product:::132
product:::89
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.