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

Hi, Having a problem with XML::Simple. In a nutshell if I try and output an xml file containing a node more that 3 levels deep XML::Simple stops producing the correct output and starts inserting "<name>" nodes. Not sure if some option or another stops the behaviour but somethings not right, (my intend use of XML::Simple is to simply output a hash produced by a sub() as an xml stream, use XMLin below just to test).

Here's a demo of the behaviour, I read in a xml file and would expect XMLout to replicate.

#Read in raw xml, where test.xml is: #<test><level1><level2><level3>testme</level3></level2></level1></test +> my $xs = new XML::Simple; my $ref = $xs->XMLin("test.xml"); #Use dumper to show the data structure created by XMLin print Dumper($ref); #Complete the circle and output the xml print $xs->XMLout($ref,NoAttr => 1,RootName=>'test');

The correct output would be a replica of test.xml, e.g.

<test> <level1> <level2> <level3>testme</level3> </level2> </level1> </test>

The actual output is XMLout:

<test> <level1> <name>level2</name> <level3>testme</level3> </level1> </test>

Would seem that despite the dumped data structure produced by XMLin being correct.

$VAR1 = { 'level1' => { 'level2' => { 'level3' => 'testme' } } }

'level3' is not wrapped within 'level2', instead the hashkey's value is embedded within a 'name' node? Can anybody suggest why?

Many thanks for any advice!

Replies are listed 'Best First'.
Re: XML::Simple Error
by kcott (Archbishop) on Nov 15, 2010 at 15:40 UTC

    Try using XML::Simple STRICT MODE. The documentation says: "the following common mistakes will be detected and treated as fatal errors: ...".

    -- Ken

      Thanks so much kcott! All fixed. Saved me so much work. Read about every option over and over, just didn't pay enough attention to strict mode, my bad!