【发布时间】:2014-10-25 17:36:12
【问题描述】:
我需要一些帮助来解析 xml 文件。这是我第一次做这种任务,我会很感激一些建议或帮助。我有一个像这样的大文件:
<Response success="true" start_row="0" num_rows="100" total_rows="100">
<ncbi-genes>
<ncbi-gene>
<acronym>Accn1</acronym>
<alias-tags>BNC1 BNaC1 ACIC2 ASIC2 Mdeg BNaC1a</alias-tags>
<data-sets>
<data-set>
<blue-channel nil="true"/>
<delegate type="boolean">true</delegate>
<specimen>
<chemotherapy nil="true"/>
<donor-id type="integer">9456</donor-id>
<donor>
<age-id type="integer">1</age-id>
<condition-description>TS26</condition-description>
<age>
<age-group-id type="integer">1</age-group-id>
<days type="float">18.5</days>
</age>
</donor>
</specimen>
<differential-expression-rankings type="array">
<differential-expression-ranking>
<structure>
<acronym>PPH</acronym>
<name>prepontine hindbrain</name>
</structure>
</differential-expression-ranking>
<differential-expression-ranking>
<structure>
<acronym>p3</acronym>
<name>prosomere 3</name>
</structure>
</differential-expression-ranking>
</differential-expression-rankings>
</data-set>
<data-set>
(...same fields as before...)
</data-set>
</data-sets>
</ncbi-gene>
</ncbi-genes>
我需要提取:
1) {ncbi-genes} -> {ncbi-gene}->{acronym}
2) {ncbi-genes} -> {ncbi-gene}->{data-sets}->{data-set}->{specimen}-{donor}->{年龄}->{天}
3) {ncbi-genes} -> {ncbi-gene}->{data-sets}->{data-set}->{{{structure}->{name}
该文件包含 100 个字段,每个字段中可以出现多次。
我尝试过的...:
#!/usr/bin/perl -w
use strict;
use warnings;
#use XML::Parser;
use LWP::Simple; # used to fetch the chatterbox ticker
use XML::Simple;
use Data::Dumper;
my $file1 = 'file.xml';
my $xml = new XML::Simple;
my $data = $xml->XMLin($file1, ForceArray => 1);
print Dumper($data); ## This prints all data OK
#To print the acronym field
foreach my $genelist (@{$data->{ncbi-genes}}) {
print $genelist;
my $curr_gene= $genelist->{ncbi-gene};
print $curr_gene->{acronym} . "\n"
}
这个循环不起作用。我认为是因为 ncbi-genes 中的“-”。我已将此字段更改为 NCBIGENES,现在错误是:
Not a HASH reference at xml_parser.pl line 19.
HASH(0x29d7ca0)
调用哈希的问题... 正如我所说,我是这种数据的新手,这是我第一次使用 xml 模块。因此,非常感谢任何有关定位自己的建议。
提前致谢。
【问题讨论】:
-
这个脚本没有26行,是哪一行出错了?
-
您是否阅读过XML::Simple 中的“该模块的状态”部分?
-
旁注:使用引号
$genelist->{'ncbi-gene'},以避免重命名元素。 -
@cucurbit XML::Twig 相当用户友好——您可能会发现开始使用 XML::LibXML 会更好。
-
“巨大”毫无意义。它可以表示从 1Mb 到 100Gb 的任何内容。有很大的不同,它可能会影响您的操作方式。