perl XML::Simple 打印问题 [英] perl XML::Simple print issue

查看:71
本文介绍了perl XML::Simple 打印问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的 xml 文件:

This my xml file :

数据.xml

<?xml version="1.0" encoding="UTF-8"?>
<ns1:BoardTestXMLExport numberOfIndictedComponents="6" testerTestStartTime=".00000:00" testTime=".00000:00" repairStationId="vvts03" testStatus="Reviewed Passed" testerTestEndTime=".00000:00" xmlns:ns1="http://tempuri.org/BoardTestXMLExport.xsd" numberOfIndictedPins="0" numberOfComponentsTested="113" numberOfJointsTested="0" numberOfDefects="6" repairStatus="Reviewed Passed">
          <ns1:StationXML testerName="huaemsaoi004" stage="V510"/>
          <ns1:TestXML name="d1">
                    <ns1:IndictmentXML algorithm="bep-3a98730h01l-lf" indictmentType="OCV Fail">
                              <ns1:RepairActionXML repairOperator="1066689" repairTime="2017-11-24T11:35:32.000+08:00" repairActionType="-" indictmentType="OCV Fail" comment="-" repairStatus="False Call"/>
                              <ns1:ComponentXML packageId="EP-3A98730H01L-LF" partId="EP-3A98730H01L-LF" designator="d1"/>
                    </ns1:IndictmentXML>
          </ns1:TestXML>
          <ns1:TestXML name="r2">
                    <ns1:IndictmentXML algorithm="rep-as30112h42l-lf" indictmentType="Right off">
                              <ns1:RepairActionXML repairOperator="1066689" repairTime="2017-11-24T11:35:32.000+08:00" repairActionType="-" indictmentType="Right off" comment="-" repairStatus="False Call"/>
                              <ns1:ComponentXML packageId="EP-AS30112H42L-LF" partId="EP-AS30112H42L-LF" designator="r2"/>
                    </ns1:IndictmentXML>
          </ns1:TestXML>
</ns1:BoardTestXMLExport>

我用下面的代码打印了IndictmentType"和name",但是打印不出来,代码如下:

I used follow code to print the "IndictmentType"and "name", but it fail to print them, code as below:

# use module
 use strict;
 use warnings;
 use XML::Simple;
 use Data::Dumper;
 # create object
 my $xml = XML::Simple->new;
# read XML file
 my $data = $xml->XMLin("data.xml",KeyAttr=>"ns1");
 print Dumper($data);
 print $data ->{"ns1:TestXML"}->{"ns1:IndictmentXML"}->{"IndictmentType"};
 print $data ->{"ns1:TestXML"}->{"ns1:IndictmentXML"};

推荐答案

当我在我的机器上尝试时,您的代码有效.当然,因为您发布了代码的图片而不是将其作为文本发布(我不知道为什么人们认为这很有用 - 您只是让您要求帮助您的人感到烦恼!),我不得不重新 -输入您的代码,我可能不小心"修复了导致您问题的任何拼写错误 - 我怀疑您尝试打印的密钥的大小写不匹配.

Your code works when I try it on my machine. Of course, because you posted a picture of your code rather than posting it as text (I have no idea why people think that is useful - you're just annoying people who you are asking to help you!), I had to re-type your code and it's possible that I "accidentally" fixed whatever typo is causing your problem - I suspect a mis-match in capitalisation of the key you are trying to print.

了解您使用的是哪个版本的 XML::Simple 也会有所帮助.您可能遇到了一个已在较新版本中修复的错误.

It would also help to know which version of XML::Simple you are using. It's possible that you are running up against a bug that has been fixed in a more recent version.

有几点值得指出.首先,XML::Simple 的文档包含以下文本:

It's worth pointing out a few things. Firstly, the documentation for XML::Simple contains this text:

此模块的状态

不鼓励在新代码中使用此模块.其他模块是提供更直接和一致的接口.特别推荐 XML::LibXML 并且XML::Twig 是一个很好的选择.

The use of this module in new code is discouraged. Other modules are available which provide more straightforward and consistent interfaces. In particular, XML::LibXML is highly recommended and XML::Twig is an excellent alternative.

这个模块的主要问题是大量的选项(其中一些有不幸的默认值)和任意方式这些选项相互作用 - 通常会产生意想不到的结果.

The major problems with this module are the large number of options (some of which have unfortunate defaults) and the arbitrary ways in which these options interact - often producing unexpected results.

带有错误修复和文档修复的补丁是受欢迎的,但新的不太可能添加功能.

Patches with bug fixes and documentation fixes are welcome, but new features are unlikely to be added.

鉴于该声明的明确性,我无法理解为什么还有人会使用该模块进行新的开发.

Given the clarity of that statement, I can't understand why anyone would still be using that module for new development.

您应该始终在代码中使用 use strictuse warnings.第一个将强制您声明变量(使用 my).如果您避免使用 new Class 语法并使用 Class->new 代替,那么您未来的维护程序员会感谢您.

You should always have use strict and use warnings in your code. The first of those will force you to declare you variables (with my). And your future maintenance programmers will thank you if you avoid the new Class syntax and use Class->new instead.

考虑到所有这些(好吧,除了我没有替换 XML::Simple),您的代码将如下所示:

Given all of those things (well, except I haven't replaced XML::Simple), your code would look like this:

#!/usr/bin/perl

use strict;
use warnings;

use XML::Simple;
use Data::Dumper;

my $xml = XML::Simple->new;

my $data = $xml->XMLin('data.xml', KeyAttr => 'ns1');

print Dumper $data;
print $data->{repairStationId};

这段代码对我来说按预期工作(但是,你的原始代码也是如此).

This code works as expected for me (but, then, so did your original code).

这篇关于perl XML::Simple 打印问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆