2

I need to extract the strings that are placed after association rightmh= using Perl.

In this example: "0x42001dc" & "0x4200000".

Each string will be added to the same array.

<?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>
4

3 回答 3

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";
于 2013-09-24T08:53:51.613 回答
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";
于 2013-09-24T10:03:59.010 回答
-2

您可以使用正则表达式。

my @array;
open XML, "<file.xml";
while(<XML>){
 if($_ =~ /association rightmh="(.*?)"/){
  push @array, $1;
 }
}
于 2013-09-24T08:50:31.327 回答