http://www.perlmonks.org?node_id=529805


in reply to Re^4: XML Parse, Spanish Elements
in thread XML Parse, Spanish Elements

Hi jbrugger,

To explain the reasong why I am unconfortable with XML::Simple, let me show you an example:
#!/usr/bin/perl use XML::Simple; use Data::Dumper; my $xml1 =<<"eof"; <config logdir="/var/log/foo/" debugfile="/tmp/foo.debug"> <server name="sahara" osname="solaris" osversion="2.6"> <address>10.0.0.101</address> <address>10.0.1.101</address> </server> <server name="gobi" osname="irix" osversion="6.5"> <address>10.0.0.102</address> </server> <server name="kalahari" osname="linux" osversion="2.0.34"> <address>10.0.0.103</address> <address>10.0.1.103</address> </server> </config> eof my $xml2 =<<"eof"; <config logdir="/var/log/foo/" debugfile="/tmp/foo.debug"> <server attr="sahara" osname="solaris" osversion="2.6"> <address>10.0.0.101</address> <address>10.0.1.101</address> </server> <server attr="gobi" osname="irix" osversion="6.5"> <address>10.0.0.102</address> </server> <server attr="kalahari" osname="linux" osversion="2.0.34"> <address>10.0.0.103</address> <address>10.0.1.103</address> </server> </config> eof print "Structure1:\n".Dumper(XMLin($xml1,ForceArray => 1)); print "Structure2:\n".Dumper(XMLin($xml2,ForceArray => 1));


The two pieces of XML are identical exept that in the first one the attribute name (could also be id) is specified, in the second it is not. The result is a parse structure that is completely different, changing the first element from an array into a hash! This is with ForceArray turned on.
Structure1: $VAR1 = { 'debugfile' => '/tmp/foo.debug', 'server' => { 'kalahari' => { 'osversion' => '2.0.34', 'osname' => 'linux', 'address' => [ '10.0.0.103', '10.0.1.103' ] }, 'sahara' => { 'osversion' => '2.6', 'osname' => 'solaris', 'address' => [ '10.0.0.101', '10.0.1.101' ] }, 'gobi' => { 'osversion' => '6.5', 'osname' => 'irix', 'address' => [ '10.0.0.102' ] } }, 'logdir' => '/var/log/foo/' }; Structure2: $VAR1 = { 'debugfile' => '/tmp/foo.debug', 'server' => [ { 'osversion' => '2.6', 'osname' => 'solaris', 'address' => [ '10.0.0.101', '10.0.1.101' ], 'attr' => 'sahara' }, { 'osversion' => '6.5', 'osname' => 'irix', 'address' => [ '10.0.0.102' ], 'attr' => 'gobi' }, { 'osversion' => '2.0.34', 'osname' => 'linux', 'address' => [ '10.0.0.103', '10.0.1.103' ], 'attr' => 'kalahari' } ], 'logdir' => '/var/log/foo/' };

If you look at the semantics of things, why does changing an attribute name (whether it is 'name' or not) change the structure of the resulting parse tree? I find this illogical and prone to making mistakes while parsing an XML file, but maybe that's just me.
--
Cheers,
Rob