norricorp has asked for the wisdom of the Perl Monks concerning the following question:

Hi, I am using the above library for some XML. Anyway, I notice that when I dump the data some parts of the original XML are missing. For instance Processing Instructions (eg <?....?>. Also this XML element
<declaration dd:maxOccur="-1" dd:minOccur="0" dd:nul +lType="exclude" <contentDeclaration dd:minOccur="0" dd:nullType=" +exclude"/> <value dd:minOccur="0" dd:nullType="exclude"/> </declaration>
is an empty hash when dumped. But other similarly structured elements are as one would expect. Any ideas why this so? Would I be better off using XML::LibXML? Regards, John

Replies are listed 'Best First'.
Re: XML::Simple problems
by dasgar (Priest) on Dec 27, 2011 at 16:24 UTC

    I was able to get XML::Simple to read in that data just fine. However, I did notice that your opening "declaration" tag was missing the ">" at the end of that line. Not sure if that is missing in your original data or if this was a just a "cut & paste" mistake in your post.

    Here's the contents of sample.xml:

    <declaration dd:maxOccur="-1" dd:minOccur="0" dd:nullType="exclude"> <contentDeclaration dd:minOccur="0" dd:nullType="exclude"/> <value dd:minOccur="0" dd:nullType="exclude"/> </declaration>

    Here's the code I ran:

    use strict; use warnings; use XML::Simple; use Data::Dumper; my $xml = XMLin('sample.xml'); print Dumper($xml);

    And here's the output:

    $VAR1 = { 'dd:minOccur' => '0', 'contentDeclaration' => { 'dd:minOccur' => '0', 'dd:nullType' => 'exclude' }, 'value' => { 'dd:minOccur' => '0', 'dd:nullType' => 'exclude' }, 'dd:maxOccur' => '-1', 'dd:nullType' => 'exclude' };

      And by the way, passing result to XMLout produces original XML:

      XMLout($xml, RootName => 'declaration');

      returns

      <declaration dd:maxOccur="-1" dd:minOccur="0" dd:nullType="exclude"> <contentDeclaration dd:minOccur="0" dd:nullType="exclude" /> <value dd:minOccur="0" dd:nullType="exclude" /> </declaration>
      Sorry that was a cut and paste mistake. The snippet I posted is part of a much larger file. Most elements are converted to hashes and arrays but there are several which are empty. I did try reading and printing with LibXML and it did read the processing instructions and the "empty elements" that Simple had were properly completed. So perhaps that is a better alternative ....
Re: XML::Simple problems
by zwon (Abbot) on Dec 27, 2011 at 16:32 UTC
    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.

      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