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

This is an odd one. I have an XML file that I was parsing without problems using XML::Simple. When we altered the file structure a bit, the script broke. It seems what broke it was the (newly added) tag named <id>. To make things even stranger, it seems to only break when multiple containers have this <id> tag. If only one has it, the script doesn't break.

The following example illustrates the problem. It should print out the index of the last <record> node (in this case "1") but instead it gives an error. Remove or rename one of the pair of <id> tags and the script runs fine.

Any ideas?

Thanks,
Doran...
ps. I'm running perl v5.6

#! /perl/bin/perl -w use strict; use XML::Simple; my $file; { local $/=''; $file=<DATA>; } my $xml=XMLin("$file"); my $progress = $xml->{dbf}{progress}{record}; print $#$progress; __END__ <?xml version="1.0" standalone="yes"?> <escinfo> <dbf> <progress> <record> <title>11111</title> <id>abcd</id> </record> <record> <title>22222</title> <id>efgh</id> </record> </progress> </dbf> </escinfo>

Replies are listed 'Best First'.
Re: XML parsing trouble with id tags
by btrott (Parson) on Aug 02, 2000 at 04:12 UTC
    I believe that "id" is a special tag for XML::Simple. It causes the code that builds the data structure to build a hash instead of an array. So, in your case, that variable $progress is actually a hash reference, not an array reference.

    This can be very useful, and you may want to see whether it helps you out by printing out the entire data structure.

    If you find that you can't stand it, you can turn it off:

    my $xml = XMLin($file, keyattr => []);
Re: XML parsing trouble with id tags
by chip (Curate) on Aug 02, 2000 at 10:19 UTC
    Unrelated to the XML issue, I notice that you're setting paragraph mode by assigning the empty string to $/. Don't you want slurp mode, which reads everything at once? To get that, just say local $/ to make $/ undefined.

        -- Chip Salzenberg, Free-Floating Agent of Chaos

      Oops, you're right. I was a bit too much in a hurry to illustrate my problem. In the real world, I'm reading in an external XML file.