所以我在 perl 中有这段代码来解析 xml,我希望 perl 文件打印到文本文件中,以便所有内容都在列中,如下所示:
SpectrumIndex TotalIonCount Duration Spectrum
Integer Float Float mass,abundance;mass1,abundance1;mass 2,abundance1
这是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;mass6,abundance6</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 代码:
#!/usr/bin/perl-w
#example to write to text
my $file = "gapiformat.txt";
unless(open FILE, '>'.$file) {
die "\nUnable to create $file\n";
}
use strict;
use warnings;
use XML::Simple;
use Data::Dumper;
my $values= XMLin('samplegapi.xml',ForceArray => ['scan-result','result-file-header']);
my $results = $values->{'cmd'}->{'scan-results'}->{'scan-result'};
my $results1= $values->{'cmd'}->{'result-file-header'};
printf FILE ("%-s %-s %-s %s\n","SpectrumIndex","TotalIonCount","Spectrum","Duration");
for my $data (@$results) {
my $spectrum=$data->{spectrum};
for my $data1 (@$results1){
printf FILE ("%-s %-s %-s %-s\n",$data->{"spectrum-index"}, $data->{tic}, $spectrum
->{'content'},$data1->{duration});
}
}
它为文本文件提供以下输出:
SpectrumIndex TotalIonCount Spectrum Duration
Integer Float mass,abundance;mass1,abundance1;
mass2,abundance2;mass6,abundance6 Float
Integer Float mass3,abundance3;mass4,abundance4;
mass5,abundance5 Float
任何帮助表示赞赏!