"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) HerongYang.com. All Rights Reserved.
#
   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":

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

 Image and Picture Processing

 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

 CGI.pm Module for Building Web Pages

 LWP::UserAgent and Web Site Testing

 Converting Perl Script to Executable Binary

 Managing Perl Engine and Modules on macOS

 Archived Tutorials

 References

 Full Version in PDF/EPUB