Can't get 'content' element to fold correctly in XML::Simple

"Mark Knoop" <[email protected]>
Newsgroups gmane.comp.lang.perl.xml
Message-ID <[email protected]>
Hi

Take the following xml

<?xml version="1.0" encoding="UTF-8" ?>
<data>
<content>
<id>1</id>
<info>someinfo</info>
</content>
<content>
<id>2</id>
<info>somemoreinfo</info>
</content>
</data>

If I XMLin it with no options it becomes (using Data::Dumper)

$VAR1 = {
          'content' => {
                       '1' => {
                              'info' => 'someinfo'
                            },
                       '2' => {
                              'info' => 'somemoreinfo'
                            }
                     }
        };

Which is what I want.

However if there is only one content tag eg

<?xml version="1.0" encoding="UTF-8" ?>
<data>
<content>
<id>1</id>
<info>someinfo</info>
</content>
</data>

try as I might I just get

$VAR1 = {
          'content' => {
                       'info' => 'someinfo',
                       'id' => '1'
                     }
        };

whereas what I really want is

$VAR1 = {
          'content' => {
                    '1' => {
                           'info' => 'someinfo',
                      }
              }
        };

I have tried using options such as

my $xml_ref = XMLin('singleton.xml',
    ForceArray => ['content'],
    KeyAttr => ['id']
);

or even

my $xml_ref = XMLin('singleton.xml',
    KeyAttr => {content=>'id'},
    ForceArray => ['content'],
);

but still I can't get it to fold on content => id

I am guessing it is something weird to do with 'content' as when I change 
the element name to something else

<?xml version="1.0" encoding="UTF-8" ?>
<data>
<test>
<id>1</id>
<info>someinfo</info>
</test>
</data>

and use the following options

my $xml_ref = XMLin('singleton.xml',
    KeyAttr => {test=>'id'},
    ForceArray => ['test'],
);

I get

$VAR1 = {
          'test' => {
                    '1' => {
                           'info' => 'someinfo'
                         }
                  }
        };

which IS what I want.

Is there a workaround with the 'content' element (other than getting the XML 
provider to change their spec)?

Thanks
Mark


_______________________________________________
Perl-XML mailing list
[email protected]
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.