免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 53343 | 回复: 4

如何把ARRAY(0x501ca8)中的数据打印出来 [复制链接]

论坛徽章:
0
发表于 2017-07-04 17:36 |显示全部楼层
本帖最后由 hztj2005 于 2017-07-04 20:43 编辑
  1. use Test;

  2. use XML::XPath;

  3. my $xp = XML::XPath->new(filename => 'customer_order.xml');

  4. my @nodes = $xp->findnodes('/order');

  5. my $ref1= @nodes[0];

  6. print  ${$ref1}."\n";
复制代码


执行上面代码,输出一个数组的地址,我希望把其中的数据print出来,该如何操作?

D:\Strawberry\plcd>perl order.pl

XML::XPath::Node::ElementImpl=ARRAY(0x501ca8)

就是把ARRAY(0x501ca8)中的数据打印出来。

下面是customer_order.xml文件

  1. <?xml version="1.0" standalone="yes"?>
  2. <order>
  3. <customer>
  4.   <name>Coyote, Ltd.</name>
  5.   <shipping_info>
  6.     <address>1313 Desert Road</address>
  7.     <city>Nowheresville</city>
  8.     <state>AZ</state>
  9.     <zip>90210</zip>
  10.   </shipping_info>
  11. </customer>
  12. <item>
  13.   <product id="1111">Acme Rocket Jet Pack</product>
  14.   <quantity type="each">1</quantity>
  15. </item>
  16. <item>
  17.   <product id="2222">Roadrunner Chow</product>
  18.   <quantity type="bag">10</quantity>
  19. </item>
  20. </order>
复制代码



论坛徽章:
145
技术图书徽章
日期:2013-10-01 15:32:13戌狗
日期:2013-10-25 13:31:35金牛座
日期:2013-11-04 16:22:07子鼠
日期:2013-11-18 18:48:57白羊座
日期:2013-11-29 10:09:11狮子座
日期:2013-12-12 09:57:42白羊座
日期:2013-12-24 16:24:46辰龙
日期:2014-01-08 15:26:12技术图书徽章
日期:2014-01-17 13:24:40巳蛇
日期:2014-02-18 14:32:59未羊
日期:2014-02-20 14:12:13白羊座
日期:2014-02-26 12:06:59
发表于 2017-07-04 19:02 |显示全部楼层
回复 1# hztj2005

try this ?

my $ref1 = $nodes[0];
print $ref1->toString, "\n";

评分

参与人数 1信誉积分 +10 收起 理由
hztj2005 + 10 很给力!

查看全部评分

论坛徽章:
0
发表于 2017-07-05 10:49 |显示全部楼层
这个要掌握方法,打出来是对象的名字和地址 XML::XPath::Node::ElementImpl=ARRAY(0x501ca,  XML::XPath::Node::Element 是包名

然后我们需要去官方文档看

https://metacpan.org/release/XML-XPath

https://metacpan.org/pod/XML::XPath::Node::Element

论坛徽章:
0
发表于 2017-07-05 12:26 |显示全部楼层
quanpai 发表于 2017-07-05 10:49
这个要掌握方法,打出来是对象的名字和地址 XML::XPath::Node::ElementImpl=ARRAY(0x501ca,  XML::XPath ...


谢谢!上面有解说,要是给出例子就更直观了。
toString ( [ norecurse ] )
Output (and all children) the node to a string. Doesn't process children if the norecurse option is a true value.

  1. sub toString {
  2.     my ($self, $norecurse) = @_;

  3.     my $string = '';
  4.     if (! $self->[node_name] ) {
  5.         # root node
  6.         return join('', map { $_->toString($norecurse) } @{$self->[node_children]});
  7.     }

  8.     $string .= "<" . $self->[node_name];
  9.     $string .= join('', map { $_->toString } @{$self->[node_namespaces]});
  10.     $string .= join('', map { $_->toString } @{$self->[node_attribs]});

  11.     if (@{$self->[node_children]}) {
  12.         $string .= ">";

  13.         if (!$norecurse) {
  14.             $string .= join('', map { $_->toString($norecurse) } @{$self->[node_children]});
  15.         }

  16.         $string .= "</" . $self->[node_name] . ">";
  17.     }
  18.     else {
  19.         $string .= " />";
  20.     }

  21.     return $string;
  22. }
复制代码


求职 : 软件工程师
论坛徽章:
3
程序设计版块每日发帖之星
日期:2015-10-07 06:20:00程序设计版块每日发帖之星
日期:2015-12-13 06:20:00程序设计版块每日发帖之星
日期:2016-05-05 06:20:00
发表于 2018-11-12 07:39 |显示全部楼层
打印引用,直接用 YAML 模块的 Dump, 或者是 JSON 模块的 encode_json:

  1. use YAML qw(Dump);
  2. use JSON qw(encode_json);

  3. # my $ref ....
  4. print Dump($ref);
  5. print encode_json($ref);
复制代码
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP