This section provides a tutorial example on how to use the 'keyattri' option to promote values of specified attributes to be hash keys. The default is 'keyattr => [name, key, id]'.
Another interesting option is "keyattr => list", which applies
to XMLin() and XMLout() to name attributes,
or sub-elements as keys to be used to promote the parent element from
array to hash. Remember that there is default list: "name", "key", and "id".
The following program shows you how to use this option, keyattr:
#- XmlSimpleKey.pl
#- Copyright (c) 1999 by Dr. Herong Yang, http://www.herongyang.com/
#
use XML::Simple;
use Data::Dumper;
my $xs = new XML::Simple(keeproot => 1,searchpath => ".",
forcearray => 1); # default is: keyattr => [name, key, id])
my $ref = $xs->XMLin("bank.xml");
my $xml = $xs->XMLout($ref);
print "\nHash dump with 'keyattr => [name, key, id]':\n";
print Dumper($ref);
print "\nXML output with 'keyattr => [name, key, id]':\n";
print $xml;
my $xs = new XML::Simple(keeproot => 1,searchpath => ".",
forcearray => 1, keyattr => [key, tag]);
my $ref = $xs->XMLin("bank.xml");
my $xml = $xs->XMLout($ref);
print "\nHash dump with 'keyattr => [key, tag]':\n";
print Dumper($ref);
print "\nXML output with 'keyattr => [key, tag]':\n";
print $xml;
exit;
Be careful, there is a default setting for "keyattr": [name, key, id].
If an attribute is found in the key list, the it's name will be removed,
and it's value will be converted into a key in the parent hash.
Note that we have 'ARRAY(0x26426ec)' in the out. The reason is that we
have a sub-element with the name "name", which is listed in "keyattr".
When a sub-element is found in the key list, it will be promoted to the
attribute level. But this will cause
a problem on the value part. The content of the sub element is converted into array
first, then this array is used as the value of this promoted attribute.
Since this promoted attribute is converted into a key in the parent hash,
the array converted from the content will be used as the key.
This is why you see 'ARRAY(0x26426ec)'.