Skip Menu |

This queue is for tickets about the XML-Simple CPAN distribution.

Report information
The Basics
Id: 5372
Status: resolved
Priority: 0/
Queue: XML-Simple

People
Owner: Nobody in particular
Requestors: peter_stubbs [...] non.agilent.com
Cc:
AdminCc:

Bug Information
Severity: Normal
Broken in:
  • 1.06
  • 2.09
Fixed in: (no value)



Subject: attribute name changed
Environment: XML::Simple version 2.09 or 1.06 (I happen to have both to hand) HP-UX 11.00 Perl v5.6.1 built for PA-RISC1.1 Source XML (test.xml): <opt> <location> <name>Location1</name> <lab> <name>lab1</name> <host> <site>4</site> <hostname>s005b01lm</hostname> <gateway>10.225.5.1</gateway> <subnetMask>255.255.255.192</subnetMask> <IPaddress>10.225.5.39</IPaddress> </host> <host> <site>4</site> <hostname>s006b01lm</hostname> <gateway>10.225.6.1</gateway> <subnetMask>255.255.255.192</subnetMask> <IPaddress>10.225.6.39</IPaddress> </host> </lab> </location> </opt> Perl Source: #!/usr/bin/perl -w use XML::Simple; use strict; use warnings; my $file = 'test.xml'; my $xml = XMLin($file, forcearray => ['hostalias', 'location', 'lab', 'addressRange'], keyattr => { location => 'name', lab => 'name', host => 'IPaddress'}); print XMLout($xml, noattr => 1) . "\n"; Output : <opt> <location> <lab> <host> <site>4</site> <hostname>s005b01lm</hostname> <gateway>10.225.5.1</gateway> <subnetMask>255.255.255.192</subnetMask> <name>10.225.5.39</name> </host> <host> <site>4</site> <hostname>s006b01lm</hostname> <gateway>10.225.6.1</gateway> <subnetMask>255.255.255.192</subnetMask> <name>10.225.6.39</name> </host> <name>lab1</name> </lab> <name>Location1</name> </location> </opt> Problem: All tags <IPaddress></IPaddress> have been changed to <name></name> and I can't see why. It's not a big problem but it looks like a bug.
The simple solution to your problem is to provide the same value for the keyattr option to XMLout as you provided to XMLin: print XMLout($xml, noattr => 1, keyattr => { location => 'name', lab => 'name', host => 'IPaddress'}); The cause is that you specifically asked XMLin to fold the list of hosts into an array keyed on IPaddress; but when you called XMLout, you relied on the default value of keyattr to unfold the hash of hashes. When reading, the IPaddress tag name was discarded, but when writing you did not provide sufficient information to allow XML::Simple to reconstruct the discarded information. See more info here: http://www.perlmonks.org/index.pl?node_id=218480 Secondly, when you need to supply the same options to XMLout as XMLin then you might find it more convenient to use the OO style: my $xs = XML::Simple->new( forcearray => ['hostalias', 'location', 'lab', 'addressRange'], keyattr => { location => 'name', lab => 'name', host => 'IPaddress'}, noattr => 1, ); my $xml = $xs->XMLin($file); print $xs->XMLout($xml); Regards Grant