[ale] XML::Simple - array folding?

Jim Lynch ale_nospam at fayettedigital.com
Tue Jan 15 07:25:36 EST 2008


Chris Woodfield wrote:
> 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
>
> _______________________________________________
> Ale mailing list
> Ale at ale.org
> http://www.ale.org/mailman/listinfo/ale
>
I've had the same experience.  I just program around it.  I test to see 
if an element is an array or a hash and act accordingly.

Jim.



More information about the Ale mailing list