[ale] XML::Simple - array folding?

Chris Woodfield rekoil at semihuman.com
Mon Jan 14 19:45:09 EST 2008


OK, second perl question in a week:

I'm trying to use XML::Simple to pulllin an XMl config file as a hash  
for parsing. What's strange is that the resulting array looks  
different depending on whether there is one or more than one element  
under a reference.

For example, here's my XML:

<hosts>
	<host>
		<name>cache1.cachesite.com</name>
		<origin>www.cache1.com</origin>
	</host>
	<host>
		<name>cache2.cachesite.com</name>
		<origin>www.cache2.com</origin>
	</host>
</hosts>
<paths>
	<path>
		<name>cache1</name>
		<origin>www.cache1.com</origin>
	</path>
</paths>

And using KeyAttr=>"name" on the XML import, the resulting hash looks  
like this:

$VAR1 = {
           'hosts' => {
                      'host' => {
                                'cache2.cachesite.com' => {
                                                          'origin' => 'www.cache2.com 
'
                                                        },
                                'cache1.cachesite.com' => {
                                                          'origin' => 'www.cache1.com 
'
                                                        }
                              }
                    },
           'paths' => {
                      'path' => {
                                'origin' => 'www.cache1.com',
                                'name' => 'cache1'
                              }
                    }
         };

The problem is that when there is only one <path> element, a 'name'  
key is added, but when there is more than one (as you can see with the  
<host> elements), the key is the <name> attribute (as I want it). Any  
know how to get XML::Simple to override this and treat a single nested  
element the same as it treats multiple nested elements?

Thanks,

-Chris



More information about the Ale mailing list