【问题标题】:Getting at XML tree data in perl在 perl 中获取 XML 树数据
【发布时间】:2015-01-05 14:19:06
【问题描述】:

我需要使用 perl 解析一个 XML 文件,我可以使用 XML::Simple 模块加载该文件,但在 XML 树中有一个标签,我无法使用 DataDumper 模块看到它,但我可以看到它的值而是。

    <testcase id="10">
        .
        .
        .
    </testcase>

上面是带有 testcase 标记的 XML 文件的示例。这是我有困难的部分。使用 DataDumper 查看数组的内容,我看到如下内容:

$VAR1 = {
          'testcases' => {
                         'file' => 'testcases.xml',
                         'testcase' => {
                                       '10' => {
                                                },

既然 XML 是这样定义的,为什么不将它放在包含 id 的 VAR1 数组中?我没有期待 testcases->testcase->id,而是得到了 testcases->testcase->10。哪个 10 是 id,但 'id' 标签发生了什么?

【问题讨论】:

  • id 不是tag,而是attribute
  • 我建议改为查看XML::Twig
  • 感谢您的建议。 XML::Twig 看起来很有趣,我可能会研究一下。

标签: perl xml-simple


【解决方案1】:

那是因为默认配置包括

KeyAttr => [qw( name key id )]

指定

KeyAttr => []

将导致id 与任何其他属性没有区别。

【讨论】:

  • 你说得对,它是一个属性。男孩,我有一段时间没有使用 XML。我想我需要复习一下。谢谢指点。
猜你喜欢
  • 2022-01-28
  • 1970-01-01
  • 2022-01-25
  • 1970-01-01
  • 2013-12-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-09-30
相关资源
最近更新 更多