【问题标题】:XML::Dumper using wrong hash reference in outputXML::Dumper 在输出中使用错误的哈希引用
【发布时间】:2012-11-11 18:38:40
【问题描述】:

我正在尝试使用 XML::Dumper 序列化以下数据结构

'options_settings' => {
  'telnet.distinct.enable' => {
    'text' => 'Option telnet.distinct.enable needs to be set to \'on\' as of
 workaround for Bug 476803',
    'severity' => '7'
   }
 },
'EOS_details' => {
  '338' => bless( {
    'info' => '<a href="https://support.netapp.com/info/communications/ECMP1110462.html " target="_blank">  CPC-0705-02 </a>',
    'count' => '48',
    'ASUP_id' => 'AE20121117202086',
    'part' => 'ESH2 - X5511A-RC, X5511-RC-C',
    'category' => 'I/O Module',
    'EOS_date' => '06/02/2013',
    'severity' => '8'
   }, 'EOSObject' ),

问题在于,当我使用 XML:Dumper 将其解析为 xml 时,它对 2 个单独的哈希引用使用相同的内存地址:

  <item key="338">
   <hashref blessed_package="EOSObject" memory_address="0x295b5758">
    <item key="ASUP_id">AE20121117165273</item>
    <item key="EOS_date">06/02/2013</item>
    <item key="category">I/O Module</item>
    <item key="count">48</item>
    <item key="info">&lt;a href=&quot;https://support.netapp.com/info/communications/ECMP1110462.html &quot; target=&quot;_blank&quot;&gt;  CPC-0705-02 &lt;/a&gt;</item>
    <item key="part">ESH2 - X5511A-RC, X5511-RC-C</item>
    <item key="severity">8</item>
   </hashref>
  </item>
 </hashref>
<item key="options_settings">
 <hashref memory_address="0x295b5320">
  <item key="telnet.distinct.enable">
   <hashref memory_address="0x295b5758">
   </hashref>
  </item>
 </hashref>
</item>

注意 memory_address="0x295b5758"。

所以当从文件中读回 option_settings 哈希引用指向 EOS 对象时:/

这是 XML::Dumper 中的错误还是我做错了什么?使用最新的 XML::Dumper 0.81

附: 我试图在主脚本之外重现它并且它有效。 我仍然无法理解主脚本中的数据是如何被破坏的。 这是使用 XML::Dumper 的代码:

    DEBUG("Before serialization: " . Data::Dumper::Dumper($result));
    my $dump = new XML::Dumper;
    my $dump_test = new XML::Dumper;
    my $test_xml = $dump_test->pl2xml ($result);
    DEBUG("After serialization in memory: " . Data::Dumper::Dumper($test_xml));
    $dump->pl2xml( $result, $filename );

结果打印正确。 “options_settings”是单独的条目。在 $test_xml 中已经和 EOS_details 混在一起了

【问题讨论】:

    标签: perl hash xml-parsing xml-serialization hashref


    【解决方案1】:

    我正在尝试复制您的问题,但没有成功。

    #!/usr/bin/perl -Tw
    
    use strict;
    use warnings;
    use XML::Dumper;
    
    my $eos = bless {
        'info'     => '<a href="https://support.netapp.com/info/communications/ECMP1110462.html " target="_blank">  CPC-0705-02 </a>',
        'count'    => '48',
        'ASUP_id'  => 'AE20121117202086',
        'part'     => 'ESH2 - X5511A-RC, X5511-RC-C',
        'category' => 'I/O Module',
        'EOS_date' => '06/02/2013',
        'severity' => '8'
        }, 'EOSObject';
    
    my %data = (
        'options_settings' => {
            'telnet.distinct.enable' => {
                'text'     => 'Option telnet.distinct.enable needs to be set to \'on\' as of
     workaround for Bug 476803',
                'severity' => '7'
            }
        },
        'EOS_details' => { 338 => $eos }
    );
    
    print pl2xml( \%data );
    

    我的程序的输出:

    <perldata>
     <hashref memory_address="0x253fb18">
      <item key="EOS_details">
       <hashref memory_address="0x2517e08">
        <item key="338">
         <hashref blessed_package="EOSObject" memory_address="0x24f9998">
          <item key="ASUP_id">AE20121117202086</item>
          <item key="EOS_date">06/02/2013</item>
          <item key="category">I/O Module</item>
          <item key="count">48</item>
          <item key="info">&lt;a href=&quot;https://support.netapp.com/info/communications/ECMP1110462.html &quot; target=&quot;_blank&quot;&gt;  CPC-0705-02 &lt;/a&gt;</item>
          <item key="part">ESH2 - X5511A-RC, X5511-RC-C</item>
          <item key="severity">8</item>
         </hashref>
        </item>
       </hashref>
      </item>
      <item key="options_settings">
       <hashref memory_address="0x2517688">
        <item key="telnet.distinct.enable">
         <hashref memory_address="0x2517598">
          <item key="severity">7</item>
          <item key="text">Option telnet.distinct.enable needs to be set to &apos;on&apos; as of
     workaround for Bug 476803</item>
         </hashref>
        </item>
       </hashref>
      </item>
     </hashref>
    </perldata>
    

    我倾向于认为您的程序有问题。 :(

    【讨论】:

    • 我是更大数据结构的一部分。我将尝试在我使用的主要代码之外重现它。
    • 好的。如果我在单独的脚本中执行它,它可以工作:/ 其余代码似乎有问题......
    • @user1847362 你最终找到问题的根源了吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-09-30
    • 2013-06-07
    • 1970-01-01
    • 2017-12-20
    • 2012-03-30
    • 2014-07-27
    • 2021-09-23
    相关资源
    最近更新 更多