如何在xml文件中提取值?

时间:2017-09-20 08:48:22

标签: xml perl

我想从XML文件中提取一些字符串。 我的XML文件如下: -

<PartNumber name="750">
    <SubComponent name="FPGA">
        <SubComponentItem name="0" device_name="golden" desc="GPCAM FPGA Golden Image" rev="0x002a0023" type="FPGA_T6E_PIC" cache="yes" />
        <SubComponentItem name="1" device_name="user"   desc="GPCAM FPGA User Image"   rev="0x002a0023" type="FPGA_T6E_PIC" cache="yes" />
    </SubComponent>
    <SubComponent name="LTC">
        <SubComponentItem name="0" desc="ltc3880-1.0v-0" rev="0x0003" type="PMBUS_T6E_QSFP28" device_name="ltc3880-1.0v" index="0xb4" />
        <SubComponentItem name="1" desc="ltc3880-3.3v"   rev="0x0003" type="PMBUS_T6E_QSFP28" device_name="ltc3880-3.3v" index="0xb4" />
    </SubComponent>
    <SubComponent name="EEPROM">
        <SubComponentItem name="0"  desc="BCM8238X Retimer 0 ver"       device_name="SLOT_NUMBER/%SLOT_NUMBER/0"  rev="D00E"      type="BCM8238X_EEPROM" cache="yes" />
        <SubComponentItem name="1"  desc="BCM8238X Retimer 0 checksum"  device_name="SLOT_NUMBER/%SLOT_NUMBER/0"  checksum="600D" type="BCM8238X_EEPROM" cache="yes" />
        <SubComponentItem name="2"  desc="BCM8238X Retimer 1 ver"       device_name="SLOT_NUMBER/%SLOT_NUMBER/0"  rev="D00E"      type="BCM8238X_EEPROM" cache="yes" />
        <SubComponentItem name="3"  desc="BCM8238X Retimer 1 checksum"  device_name="SLOT_NUMBER/%SLOT_NUMBER/0"  checksum="600D" type="BCM8238X_EEPROM" cache="yes" />
    </SubComponent>
</PartNumber>

例如,我想在PartNumber name = 750和SubComponentItem name = FPGA中提取rev值。我怎样才能提取它并存储它。

我曾尝试过以下代码,但仍遇到一些错误,下面是我的代码: -

  use strict;
  use warnings;
  use XML::Simple;
  use Data::Dumper;

  my $simple = XML::Simple->new();
  my $data = $simple->XMLin('/cy/programable/1ProgrammableRevision.xml');

  print Dumper($data) . "\n";

  print $data->{PartNumber}->{750}->{FPGA}->{0}->{rev}->[1];

对于您的信息,我的perl版本5.8.8和XML :: libxml或XML :: Twig不适用。

2 个答案:

答案 0 :(得分:6)

不要使用XML::Simple - 使用xpath可以轻松完成此任务,为此您需要XML::LibXMLXML::Twig

#!/usr/bin/perl

use strict;
use warnings;

use XML::Twig;

my $twig = XML::Twig -> parsefile ( '/cy/programable/1ProgrammableRevision.xml'); 

my $value = $twig -> get_xpath('//PartNumber[@name="750"]/SubComponent[@name="FPGA"]/SubComponentItem[@device_name="user"]',0 ) -> att('rev');

print $value;

xpath的一个细节是你可以使用部分路径 - 假设你知道你正在寻找“FPGA_T6E_PIC”:

my $value = $twig -> get_xpath('//SubComponentItem[@type="FPGA_T6E_PIC"]',0 ) -> att('rev');
print $value;

答案 1 :(得分:-3)

用简单的正则表达式?

my $name='';

if (/<PartNumber ([^>]+)>/) {
    my $PN_attr=$1;
    if ($PN_attr =~ /name="([^"]*)"/) {
        $name=$1;
    }
}

print $name;

[^>]+方案不匹配< … >块内的结束分隔符。