【发布时间】:2014-09-24 21:55:06
【问题描述】:
我正在尝试在 perl 中解析一些 XML,但我很难弄清楚如何访问它。我仅限于使用XML::Parser 或XML::Twig,因为它必须部署在大量RedHat 6 系统上而不添加任何模块,所以我不能使用像XML::Simple 这样更方便的东西。
问题是我如何获取数据。代码的核心是这样的:
use XML::Parser;
use Data::Dumper;
# $response_string is a string of XML.
my $xml_parser = new XML::Parser(Style => 'Time');
my $xml_wodge = $xml_parser->parse($response_string);
# Export methods to see what this is.
print Dumper ($xml_wodge);
print "I have a ref type: ", ref($xml_wodge), "\n";
print "I have array size: ", @xml_wodge, "\n";
print "I have hash keys: ", %xml_wodge, "\n";
print "I have string value: ", $xml_wodge, "\n";
这会产生一些奇怪的输出(已编辑)
$VAR1 = [
[valid XML dump]
];
I have reference type ARRAY
I have array size:
I have hash keys:
I have string value ARRAY(0x22c5538)
$xml_wodge 中的值是某种东西。转储程序正确地将其转储出去,但我使用的所有数据结构原语似乎都没有返回任何内容。它检测为 Array,但它似乎是一个零元素数组,其中隐藏着完整的 XML 数据结构。
我如何到达它?
【问题讨论】:
-
好像是数组引用。见References tutorial。
-
始终使用
use strict; use warnings;!$xml_wodge和@xml_wodge是不同的变量。 -
XML::Simple本身的module documentation 不鼓励使用,因此我反对将其归类为“方便”。在这两个选项中,我建议使用XML::Twig,尽管它取决于版本号。 -
您一直在说您无法安装模块,但您的代码依赖于 XML::Parser::Style::Time,这在 CPAN 上甚至都不存在。
-
XML::Parser 和 XML::Twig 都不是核心模块(包含在 Perl 中)。如果您从操作系统包存储库中获取它们,那么我建议您从同一个地方获取 XML::LibXML 和 using that
标签: perl xml-parsing