Perl Tutorials - Herong's Tutorial Examples
Dr. Herong Yang, Version 5.10

"keyattr" - Namings Attributes as Keys

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;

The input file, bank.xml, has the following XML:

<?xml version="1.0"?>
<bank>
 <account id="123-4567">
  <type>Checking</type>
  <balance>149.99</balance>
 </account>
 <client>
  <name>Mike Lee</name>
  <email>mike@lee.com</email>
 </client>
 <account>
  <id>333-4444</id>
  <type>Saving</type>
  <balance>941.99</balance>
 </account>
</bank>

Here is the output of the program:

Hash dump with 'keyattr => [name, key, id]':
$VAR1 = {
   'bank' => [
      {
         'account' => {
            'ARRAY(0x26426ec)' => {
               'type' => [
                  'Saving'
                  ],
               'balance' => [
                  '941.99'
                  ]
               },
            '123-4567' => {
               'type' => [
                  'Checking'
                  ],
               'balance' => [
                  '149.99'
                  ]
               }
            },
         'client' => {
            'ARRAY(0x2642680)' => {
               'email' => [
                  'mike@lee.com'
                  ]
               }
            }
         }
      ]
   };

XML output with 'keyattr => [name, key, id]':
<bank>
  <account name="ARRAY(0x26426ec)">
    <type>Saving</type>
    <balance>941.99</balance>
  </account>
  <account name="123-4567">
    <type>Checking</type>
    <balance>149.99</balance>
  </account>
  <client name="ARRAY(0x2642680)">
    <email>mike@lee.com</email>
  </client>
</bank>

Hash dump with 'keyattr => [key, tag]':
$VAR1 = {
   'bank' => [
      {
         'account' => [
             {
               'id' => '123-4567',
               'type' => [
                  'Checking'
                  ],
               'balance' => [
                  '149.99'
                  ]
               },
            {
               'id' => [
                  '333-4444'
                  ],
               'type' => [
                  'Saving'
                  ],
               'balance' => [
                  '941.99'
                  ]
               }
            ],
         'client' => [
            {
               'email' => [
                  'mike@lee.com'
                  ],
               'name' => [
                  'Mike Lee'
                  ]
               }
            ]
         }
      ]
   };

XML output with 'keyattr => [key, tag]':
<bank>
  <account id="123-4567">
    <type>Checking</type>
    <balance>149.99</balance>
  </account>
  <account>
    <id>333-4444</id>
    <type>Saving</type>
    <balance>941.99</balance>
  </account>
  <client>
    <email>mike@lee.com</email>
    <name>Mike Lee</name>
  </client>
</bank>

Notes about "keyattr":

  • 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)'.

Table of Contents

 About This Book

 Perl on Linux Systems

 ActivePerl on Windows Systems

 Data Types: Values and Variables

 Expressions, Operations and Simple Statements

 User Defined Subroutines

 Perl Built-in Debugger

 Name Spaces and Perl Module Files

 Symbolic (or Soft) References

 Hard References - Addresses of Memory Objects

 Objects (or References) and Classes (or Packages)

 Typeglob and Importing Identifiers from Other Packages

 String Built-in Functions and Performance

 File Handles and Data Input/Output

 Open Files in Binary Mode

 Open Directories and Read File Names

 File System Functions and Operations

 Converting Perl Script to Executable Binary

 Using DBM Database Files

 Using MySQL Database Server

 Socket Communication Over the Internet

XML::Simple Module - XML Parser and Generator

 XMLin() and XMLout() Methods

 XML Parsing Options

 "forcearray" - Forcing Element Contents as Arrays

 "suppressempty" - Parsing Empty Elements

"keyattr" - Namings Attributes as Keys

 XmlSimpleHash.pl - XML Hash Example

 XML Communication Model

 SOAP::Lite - SOAP Server-Client Communication Module

 Perl Programs as IIS Server CGI Scripts

 CGI (Common Gateway Interface)

 XML-RPC - Remote Procedure Call with XML and HTTP

 RPC::XML - Perl Implementation of XML-RPC

 Integrating Perl with Apache Web Server

 References

 Printable Copy - PDF Version

Dr. Herong Yang, updated in 2009
"keyattr" - Namings Attributes as Keys