2013-05-31 63 views
1

因此,我使用Perl解析了以下XML代碼,並試圖調用譜結果,但由於它是散列,所以我很困難。我一直收到錯誤消息參考找到哪裏大小列表預期。如何處理在Perl中解析爲散列的調用數據

<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"; 
} 
} 

我希望能夠打印出頻譜值對。

+0

爲什麼perl代碼中'result-file-'和'header'之間有空格? – Barmar

+0

上傳錯誤...其實不在perl代碼 –

+0

哪一行是錯誤的? '%頻譜= $數據 - > {光譜}'? – Barmar

回答

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解析庫)。

+0

完美的非常感謝! –

相關問題