#!/usr/bin/perl
{
package XML::Simple::Ordered; # subclass overriding new_hashref
+ method
use base qw(XML::Simple);
use Tie::IxHash;
sub new_hashref {
my $self = shift;
my %hash;
tie %hash, 'Tie::IxHash', @_;
return \%hash;
}
}
package main;
my $simple = XML::Simple::Ordered->new();
my $data = $simple->XMLin($file);
new_hashref is a documented hook, and the above does in fact seem
to produce ordered results for simple inputs.
Still, no guarantees whatsoever!
Update: actually, new_hashref isn't being used consistently, so some hashes are still not ordered... (in the case of tags without any attributes)
To fix this issue, you'd unfortunately have to patch the source file XML/Simple.pm:
sub collapse {
my $self = shift;
# Start with the hash of attributes
my $attr = shift;
$attr = $self->new_hashref() unless %$attr; # <-- add this line
That's ugly, but with this additional change, most typical cases should work.
|