我需要使用perl提取在association 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>

最佳答案

使用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";

10-06 10:44