perl XML::Simple print issue [closed]

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.

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.

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

STATUS OF THIS MODULE

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.

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.

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).

Leave a Comment