【发布时间】:2016-07-18 05:15:37
【问题描述】:
我对 perl 很陌生。我正在尝试使用 perl xml 解析模块 XML::Simple 解析 xml 文件。 下面是我试图解析的示例 xml:
<?xml version="1.0" encoding="US-ASCII"?>
<book>
<key>ISBN1</key>
<str>name1</str>
<key>ISBN2</key>
<str>name2</str>
<key>ISBN3</key>
<str>name3</str>
<key>ISBN4</key>
<str>name4</str>
<key>ISBN5</key>
<str>name5</str>
<key>ISBN6</key>
<str>name6</str>
</book>
下面是我用来解析和转储解析输出的 perl 程序:
my $xml = XML::Simple->new;
my $data = $xml->XMLin($bookMap);
print Dumper($data);
输出是:
$VAR1 = {
'str' => [
'name1',
'name2',
'name3',
'name4',
'name5',
'name6'
],
'key' => [
'ISBN1',
'ISBN2',
'ISBN3',
'ISBN4',
'ISBN5',
'ISBN6'
]
};
但我需要将其解析为以下格式:
$VAR1 = {
'ISBN1' => 'name1',
'ISBN2' => 'name2',
'ISBN3' => 'name3',
'ISBN4' => 'name4',
'ISBN5' => 'name5',
'ISBN6' => 'name6',
};
看来我的 xml 格式与 perl xml 解析器所期望的格式不同。有人可以帮我找到更好的方法来获得预期的输出吗?
提前致谢。
【问题讨论】:
-
来自search.cpan.org/~grantm/XML-Simple-2.22/lib/XML/Simple.pm
You really don't want to use this module in new code. If you ignore this warning and use it anyway, the qw(:strict) mode will save you a little pain. -
XML::LibXMLsearch.cpan.org/~shlomif/XML-LibXML-2.0126/LibXML.pod 和XML::Twigsearch.cpan.org/~mirod/XML-Twig-3.49/Twig.pm 建议作为替代方案。