使用Perl从xml文件中提取字符串

时间:2013-09-24 08:35:54

标签: xml perl xml-parsing

我需要使用Perl提取关联rightmh = 后面的字符串。

在此示例中:“0x42001dc”& “0x4200000”。

每个字符串都将添加到同一个数组中。

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<association-response-list xmlns="http://url.com">
<association-responses>
<association rightmh="0x42001dc" leftmh="0x4055246" rh="0x1003b"/>
<association rightmh="0x4200000" leftmh="0x455246" rh="0x1003b"/>
</association-responses>
</association-response-list>

3 个答案:

答案 0 :(得分:9)

使用XML解析器,例如XML::LibXML

#!/usr/bin/perl
use warnings;
use strict;

use XML::LibXML;

my $xml = << '__XML__';
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<association-response-list xmlns="http://url.com">
<association-responses>
<association rightmh="0x42001dc" leftmh="0x4055246" rh="0x1003b"/>
<association rightmh="0x4200000" leftmh="0x455246" rh="0x1003b"/>
</association-responses>
</association-response-list>
__XML__

my $doc = 'XML::LibXML'->load_xml(string => $xml);

my @rightmh;
push @rightmh, $_->value for $doc->findnodes('//@rightmh');
print "@rightmh\n";

答案 1 :(得分:3)

XML::Twig的解决方案:

#!/usr/bin/perl
use warnings;
use strict;

use XML::Twig;

my $xml = << '__XML__';
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<association-response-list xmlns="http://url.com">
<association-responses>
<association rightmh="0x42001dc" leftmh="0x4055246" rh="0x1003b"/>
<association rightmh="0x4200000" leftmh="0x455246" rh="0x1003b"/>
</association-responses>
</association-response-list>
__XML__

my @rightmh;
XML::Twig->new( twig_roots => { 'association[@rightmh]'
                                   => sub { push @rightmh, $_->att( 'rightmh'); }
                              }
              )
         ->parse( $xml);
print "@rightmh\n";

答案 2 :(得分:-2)

您可以使用正则表达式。

my @array;
open XML, "<file.xml";
while(<XML>){
 if($_ =~ /association rightmh="(.*?)"/){
  push @array, $1;
 }
}