1

所以我使用 Perl 解析了以下 XML 代码,我试图调用频谱结果,但我遇到了困难,因为它是一个哈希。我不断收到错误消息参考,发现甚至是预期大小的列表。

<message>
  <cmd id="result_data">
    <result-file-header>
      <path>String</path>
      <duration>Float</duration>
      <spectra-count>Integer</spectra-count>
    </result-file-header>
    <scan-results count="Integer">
      <scan-result>
        <spectrum-index>Integer</spectrum-index>
        <time-stamp>Integer</time-stamp>
        <tic>Float</tic>
        <start-mass>Float</start-mass>
        <stop-mass>Float</stop-mass>
        <spectrum count="Integer">mass,abundance;mass1,abundance1;
          mass2,abundance2</spectrum>
      </scan-result>
      <scan-result>
        <spectrum-index>Integer</spectrum-index>
        <time-stamp>Integer</time-stamp>
        <tic>Float</tic>
        <start-mass>Float</start-mass>
        <stop-mass>Float</stop-mass>
        <spectrum count="Integer">mass3,abundance3;mass4,abundance4;
          mass5,abundance5</spectrum>
      </scan-result>
    </scan-results>
  </cmd>
</message>

这是我正在使用的 Perl 代码:

my $file = "gapiparseddataexample1.txt";
unless(open FILE, '>'.$file) {
die "\nUnable to create $file\n";
}

use warnings;
use XML::Simple;
use Data::Dumper;
my $values= XMLin('samplegapi.xml', ForceArray => [ 'scan-result' ,'result-file-header']);
print Dumper($values);

my $results = $values->{'cmd'}->{'scan-results'}->{'scan-result'};
my $results1=$values->{'cmd'}->{'result-file-header'};

for my $data (@$results) {
 print FILE "Spectrum Index",":",$data->{"spectrum-index"},"\n";
 print FILE "Total Ion Count",":",$data->{tic},"\n";
 %spectrum=$data->{spectrum};

print FILE "Spectrum",":",%spectrum, "\n";
    for my $data1 (@$results1) {
        print FILE "Duration",":",$data1->{duration},"\n";
 }
}

我希望能够打印出频谱值对。

4

1 回答 1

1

这个:

 $spectrum=$data->{spectrum};

print FILE "Spectrum",":", $spectrum->{'content'}, "\n";
for my $data1 (@$results1) {
        print FILE "Duration",":",$data1->{duration},"\n";
}

应该给你这个(我认为这是你想要的):

Spectrum:mass,abundance;mass1,abundance1;
      mass2,abundance2

你会想从我想象的“内容”中删除换行符值(所以它不会分成两行)。

给好奇的人解释一下

元素内容已被推入“->内容”,因为元素也有一个属性。在这种情况下,称为“计数”:

<spectrum count="Integer">mass3,abundance3;mass4,abundance4;
          mass5,abundance5</spectrum>

这种行为在其他语言和其他 XML 解析库中也很常见(例如,有时他们将其塞入键为 0 的元素中)。有时即使元素没有常规属性但属于特定类型,也会发生这种情况。

如果您要 var dump $data->{$spectrum} 您会看到结构(同样,这通常也适用于其他语言和其他 XML 解析库)。

于 2013-06-01T00:43:29.583 回答