[ale] XML::Simple - array folding?

Tim Watts timtw at earthlink.net
Mon Jan 14 22:59:03 EST 2008


Hi,

Just a wild guess, but could the parsing problem be caused by the fact that 
the XML doc has multiple root elements? Or was that just omitted for brevity?


On Monday 14 January 2008 7:44 pm, 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



More information about the Ale mailing list