[ale] XML::Simple - array folding?

David Hamm ale at spinnerdog.com
Tue Jan 15 14:51:19 EST 2008


You can do this.

my( $XFer ) = XMLin( $Source, 
ForceArray=>[ 'recipients', 'uploads', 'downloads', 'controls', 'error', 'Pickup', 'Entry' ] );

But you have to know which elements should be forced to array before hand.

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