无法使用Perl从XML文件中获取值

时间:2018-11-26 05:48:11

标签: xml perl fetch

下面是xml文件:

<Build_details>
  <Release number="1902">
    <Build number="220">
      <OMS>
        <Build_path>ST_OMS_V19021_B220</Build_path>
        <Pc_version>8042.18.01</Pc_version>
      </OMS>
      <OMNI>
        <Build_path>ST_OMNI_V19021_B220</Build_path>
      </OMNI>
    </Build>
  </Release>
  <Release number="1810">
    <Build number="340">
      <OMS>
        <Build_path>ST_OMS_V1810_B340</Build_path>
        <Pc_version>8041.30.01</Pc_version>
      </OMS>
      <OMNI>
        <Build_path>ST_OMNI_V1810_B340</Build_path>
      </OMNI>
    </Build>
  </Release>
  <Release number="1810">
    <Build number="300">
      <OMS>
        <Build_path>ST_OMS_V18102_B300</Build_path>
        <Pc_version>8041.30.01</Pc_version>
      </OMS>
      <OMNI>
        <Build_path>ST_OMNI_V18102_B300</Build_path>
      </OMNI>
    </Build>
  </Release>
</Build_details>

我无法获取与其他内部版本号相同的发行版号的值。假设我要获取版本1810 build 300的详细信息,它将抛出空值。看来我写的perl脚本仅适用于首次出现的值。

下面是我的脚本:

#!/appl/OMS/perl/bin/perl
=comments
Perl wrapper for parsing the central repository
xml to fetch relevant data of each release

Help Usage : ./parseenv.pl allrel -- to list all versions\
./parseenv.pl <relno> <buildno> <oms/pc/omni>
ex - ./parseenv.pl 1810 300 oms
Date=20180910
=cut

use XML::Smart;
use File::Path qw(make_path remove_tree);

$FILE="/appl/OMS/Build_Details/build_details.xml";

$xml = XML::Smart->new($FILE) || die ("Could not open XML file");
my $relno=$ARGV[0];
my $build=$ARGV[1];

if ( $ARGV[0] =~ "allrel" ) {
    my @release=$xml->{Build_details}->{Release}('[@]','number');
    foreach $i (@release) {
        print "$i\n";
    }
}

if ($ARGV[1] =~ "build") {
    my @build=$xml->{Build_details}->{Release}->('number','eq',"$relno")->{Build}('[@]','number');
    foreach $i (@build) {
        print "$i\n";
    }
}

if ($ARGV[2] =~ "oms") {
    my @oms=$xml->{Build_details}->{Release}->('number','eq',"$relno")->{Build}('number','eq',"$build")->{OMS}->{Build_path};
    print "@oms\n";

}
elsif ($ARGV[2] =~ "pc") {
    my @pc=$xml->{Build_details}->{Release}->('number','eq',"$relno")->{Build}('number','eq',"$build")->{OMS}->{Pc_version};
    print "@pc\n";

}
elsif ($ARGV[2] =~ "omni") {
    my @omni=$xml->{Build_details}->{Release}->('number','eq',"$relno")->{Build}('number','eq',"$build")->{OMNI}->{Build_path};
    print "@omni\n";

}
else {
    print "Please enter 3rd argument"
}

输出详细信息:

第一次出现效果很好

$  ./parsebuilddetails.pl 1810 340 pc
8041.30.01

不适用于其他情况:

$ ./parsebuilddetails.pl 1810 300 pc

我如何修改perl脚本以使其适用于内部版本号的所有值。看来我将不得不添加一个循环。但是我不明白它会如何迭代。请帮忙:)

1 个答案:

答案 0 :(得分:0)

始终使用严格的警告语!只是XML :: LibXML:

的示例
#!/usr/bin/env perl                                                                                                                                                                            

use strict;
use warnings;

use XML::LibXML;

my $xml_string = join '', <DATA>;

my $dom = XML::LibXML->load_xml(string => $xml_string);


foreach my $build_node ($dom->findnodes('//Release[@number="1810"]/Build')) {
  print $build_node->to_literal;
}

__DATA__
...place your xml here

Nice tutorial for XML::LibXML