XPathによる値の取得
XML::LibXMLモジュールを使うとXPathでノードを指定して値を取得できます。
use strict;
use warnings;
use XML::LibXML;
my $file = 'data.xml';
my $xpath = '/configuration/appSettings/add';
binmode STDOUT, ":encoding(cp932)"; # Windows向け
my $doc = XML::LibXML->load_xml(location => $file);
my @nodes = $doc->findnodes($xpath);
foreach my $node (@nodes) {
my $key = $node->getAttribute('key');
my $value = $node->getAttribute('value');
printf "%s=%s\n", $key, $value;
}
__END__
上記のスクリプトはこんなXMLを対象としています。
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<appSettings>
<add key="key1" value="値1" />
<add key="key2" value="値2" />
<add key="key3" value="値3" />
</appSettings>
</configuration>
