in reply to XML::Complicated

The reason i used the keyattr in my previous reply was due to a strange quirk within XML::Simple. Consider this example:
use XML::Simple; use Data::Dumper; my $xml = XMLin(\*DATA); print Dumper $xml; __DATA__ <documents> <document> <stayonhost>1</stayonhost> <staybelow>1</staybelow> <maxdepth>2</maxdepth> <name>Swedish Foo</name> <home_url>http://www.foo.se/bar/</home_url> <category>International</category> </document> <document> <stayonhost>0</stayonhost> <staybelow>0</staybelow> <maxdepth>3</maxdepth> <name>Swedish Bar</name> <home_url>http://www.bar.se/baz/</home_url> <category>National</category> </document> </documents>
and it's output:
$VAR1 = {
  'document' => {
    'Swedish Foo' => {
      'category' => 'International',
      'maxdepth' => '2',
      'stayonhost' => '1',
      'staybelow' => '1',
      'home_url' => 'http://www.foo.se/bar/'
    },
    'Swedish Bar' => {
      'category' => 'National',
      'maxdepth' => '3',
      'stayonhost' => '0',
      'staybelow' => '0',
      'home_url' => 'http://www.bar.se/baz/'
    }
  }
};
What happened to the name element? I don't know for sure, but i am guessing that XML::Simple treats elements named name differently.* So, i read some docs, figured that keyattr might fix the problem and it did. But first i tried forcearray, which didn't fix the problem. I didn't think it would, but i didn't have a spare chicken to sacrifice at the time. ;) In case you are wondering ... forcearray is useful for keeping XML elements in order, remember that hashes lose order. forcearray is also useful for keeping your code consistent - you can always (99.99%?) expect an array, no guessing with ref needed.

I guess my point is that XML::Simple can handle tasks that are really beyond its scope, and you should be wary of that. It's great for getting stuff done quick, but you probably should pick a more industrial strength module for serious production code - like XML::Twig.

* yep ... here's what the docs say:
   The default value for 'keyattr' is ['name', 'key',
   'id'].  Setting this option to an empty list will
   disable the array folding feature.

jeffa

L-LL-L--L-LL-L--L-LL-L--
-R--R-RR-R--R-RR-R--R-RR
B--B--B--B--B--B--B--B--
H---H---H---H---H---H---
(the triplet paradiddle with high-hat)

Replies are listed 'Best First'.
Re^2: XML::Complicated (use twig)
by Aristotle (Chancellor) on May 02, 2003 at 00:39 UTC
    Just posting to second the notion to use XML::Twig. I don't even bother with XML::Simple these days.

    Makeshifts last the longest.