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