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

Owner: Nobody in particular
Requestors: peter_stubbs [...]

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

Subject: attribute name changed
Download (untitled) / with headers
text/plain 1.6k
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></gateway> <subnetMask></subnetMask> <IPaddress></IPaddress> </host> <host> <site>4</site> <hostname>s006b01lm</hostname> <gateway></gateway> <subnetMask></subnetMask> <IPaddress></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></gateway> <subnetMask></subnetMask> <name></name> </host> <host> <site>4</site> <hostname>s006b01lm</hostname> <gateway></gateway> <subnetMask></subnetMask> <name></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: 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

This service is sponsored and maintained by Best Practical Solutions and runs on infrastructure.

Please report any issues with to