【问题标题】:Content missing after converting hash to XML using XML::Simple in Perl在 Perl 中使用 XML::Simple 将哈希转换为 XML 后内容丢失
【发布时间】:2021-08-13 12:35:17
【问题描述】:

代码:

my $test = {
    'a' => {
        'disabled' => 'false',
        'options' => '%build_options',
        'mailStatus' => {}, 
        'dependencies' =>{                                                                
            'test' => {                                                                                                              
                    'platforms' => {},                                                                                                 
                    'name' => 'nightly-regressions',                                                                                    
                    'preRequisitePlatforms' => {},                                                                                                             
                    'dependType' => 'pass'
                    },
        },                                                               
        'Above' => 'false',
        'options' => {},
        'critical' => 'true',
    }
};
print XMLout($test, noattr=>1, KeepRoot=>1, RootName=>undef, NoEscape => 1);

当我运行上面的代码将哈希转换为 xml 时,缺少一级“测试”,我得到的输出 xml 是:

输出:

<a>
  <Above>false</Above>
  <critical>true</critical>
  <dependencies>
    <name>nightly-regressions</name>
    <dependType>pass</dependType>
    <platforms></platforms>
    <preRequisitePlatforms></preRequisitePlatforms>
  </dependencies>
  <disabled>false</disabled>
  <mailStatus></mailStatus>
  <options></options>
</a>

谁能帮我找出我的代码有什么问题?

【问题讨论】:

  • 使用XML::Simple 有什么好的理由吗? documentation 对你大喊请不要在新代码中使用此模块...
  • 这个答案是否:stackoverflow.com/a/60404484/3710053你的问题?
  • 不要使用 XML::Simple。只是不要!这是对时间和问题根源的巨大浪费。为什么人们不相信一个模块的文档,因为它是如此的破碎,所以会警告使用该模块?

标签: xml perl hash


【解决方案1】:

尝试使用严格选项导入 XML::Simple:

use XML::Simple qw(:strict);

您会发现您缺少 KeyAttr 选项的值:

print XMLout($test, noattr=>1, KeepRoot=>1, RootName=>undef, NoEscape=>1, KeyAttr=>[]);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-07-13
    • 2013-01-06
    • 1970-01-01
    • 2016-07-25
    • 2016-08-26
    • 2011-12-21
    • 1970-01-01
    • 2011-05-08
    相关资源
    最近更新 更多