简体   繁体   English

Perl XML ::简单的不一致行为

[英]Perl XML::Simple Inconsistent behavior

I am reading XML file using XML::Simple 我正在使用XML :: Simple阅读XML文件

However, I am facing a rather "odd" situation wherein XML::Simple is behaving inconsistently across hosts 但是,我面临一个相当“奇怪”的情况,其中XML :: Simple在主机之间的行为不一致

I can best guess that the shell has some role to play - but I can't be sure as I didn't find any such issue documented against XML::Simple 我可以最好地猜测外壳程序可以发挥某些作用-但我不确定,因为我没有发现针对XML :: Simple记录的任何此类问题

Any pointer will be a great aid in debugging this issue 任何指针将有助于调试此问题

use strict;
use warnings;
use XML::Simple;
use Data::Dumper;
sub readXml() {

    print "XML::Simple version : $XML::Simple::VERSION\n";

    my ($phRec) = eval {XMLin("sample.xml", ForceArray => 1, KeyAttr => [] )};
    if ( $@ ) {
        print (join '', $@);
        return 0;
    }
    print Dumper($phRec);
    return 1;
}

readXml();

sample.xml sample.xml

<?xml version="1.0" encoding="utf-8"?>
<node>
    <people name="whatever">etc</people>
    <people name="abc <whatever> pqr">etc</people>
</node>

I understand this is not a valid XML - but I would rather that XML::Simple should fail in both the hosts. 我知道这不是有效的XML-但我希望XML :: Simple在两个主机中均应失败。

Host1 [Development host] Host1 [开发主机]

bin: perl -v bin:perl -v

This is perl 5, version 14, subversion 1 (v5.14.1) built for x86_64-linux ... 这是为x86_64-linux构建的perl 5,版本14,版本1(v5.14.1)...

bin: echo $SHELL bin:echo $ SHELL

/bin/bash / bin / bash

bin: ./template bin:./ template

XML::Simple version : 2.18
$VAR1 = {
          'people' => [
                      {
                        'content' => 'etc',
                        'name' => 'whatever'
                      },
                      {
                        'content' => 'etc',
                        'name' => 'abc <whatever> pqr'
                      }
                    ]
        };

Host2 [ VM ] 主机2 [VM]

bash-4.1# perl -v bash-4.1#perl -v

This is perl, v5.10.1 (*) built for x86_64-linux-thread-multi... 这是为x86_64-linux-thread-multi构建的perl v5.10.1(*)...

bash-4.1# echo $SHELL bash-4.1#echo $ SHELL

/bin/csh / bin / csh

bash-4.1# ./template bash-4.1#./模板

XML::Simple version : 2.18
sample.xml:4: parser error : Unescaped '<' not allowed in attributes values
    <people name="abc <whatever> pqr">etc</people>
                      ^
sample.xml:4: parser error : attributes construct error
    <people name="abc <whatever> pqr">etc</people>
...

The XML parser used by XML::Simple on Host1 is apparently more lenient than the one Host2. Host1上的XML :: Simple使用的XML解析器显然比一个Host2宽松。


XML::Simple doesn't actually parse XML. XML :: Simple实际上不会解析XML。 It delegates that task to XML::Parser or XML::SAX . 它将任务委托给XML :: ParserXML :: SAX Even then, the latter itself delegates the parsing to one of many other modules. 即使这样,后者本身也将解析委托给许多其他模块之一。

Not all of those parsers are of the same quality. 并非所有这些解析器都具有相同的质量。

Refer to "Environment" section of XML::Simple 's documentation for more info. 有关更多信息,请参考XML :: Simple文档的“环境”部分。 That section documents a means to select the parser XML::Simple uses. 该部分记录了选择解析器XML :: Simple使用的方法。 However, you should this chance to stop using XML::Simple! 但是,您应该有机会停止使用XML :: Simple! It's so complicated to use its own documentation discourages people from using it! 使用自己的文档是如此复杂,使人们无法使用它!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM