in reply to XML::Simple problems

Any ideas why this so?

XML::Simple allows you quickly convert XML into perl structure and back, but as there's no one-to-one mapping between these things there's a lot of caveats, so in the end it is not simple at all. Just look at the number of various parameters it has ;)

Would I be better off using XML::LibXML?

Maybe, but that depends on your needs.

Replies are listed 'Best First'.
Re^2: XML::Simple problems
by cavac (Prior) on Dec 27, 2011 at 18:58 UTC

    One of the main reasons is just the flexibility of XML. And the flexibility of Perl.

    Say, you have the following XML snippet:

    <yada hello="world"></yada>
    Now, say you get as output the following:
    <yada hello="world"/>
    The XML parser/generator functions did nothing wrong, since in XML context both snippets are the same. Well mostly, since most XML parsers are probably very slightly faster parsing the second representation.

    Now take a look in the direction of Perl. You parse some XML data into a hash. Since hashes don't hold ordered data, the order of the output is not guaranteed. Depending on your XML file and how you use it, it might not matter anyway. Let's take a look at a slightly more complex example:

    <bars> <bar beer="Moe's Best" bartender="Moe">Moe's</bar> <bar beer="Unnamed" bartender="Sam">Cheers</bar> </bars>
    When you read it into a hash of hashes and write it out again, you might get:
    <bars> <bar bartender="Sam" beer="Unnamed">Cheers</bar> <bar beer="Moe's Best" bartender="Moe">Moe's</bar> </bars>
    This would still represent the same dataset. If order is important, you'll have to use arrays. XML::Simple has an option for that.

    BREW /very/strong/coffee HTTP/1.1
    Host: goodmorning.example.com
    
    418 I'm a teapot