ASPN ActiveState Programmer Network
ActiveState
/ Home / Perl / PHP / Python / Tcl / XSLT /
/ Safari / My ASPN /
Cookbooks | Documentation | Mailing Lists | Modules | News Feeds | Products | User Groups


Recent Messages
List Archives
About the List
List Leaders
Subscription Options

View Subscriptions
Help

View by Topic
ActiveState
.NET Framework
Open Source
Perl
PHP
Python
Tcl
Web Services
XML & XSLT

View by Category
Database
General
SOAP
System Administration
Tools
User Interfaces
Web Programming
XML Programming


MyASPN >> Mail Archive >> perl-xml
perl-xml
Can't get 'content' element to fold correctly in XML::Simple
by Mark Knoop other posts by this author
May 6 2009 6:12AM messages near this date
view in the new Beta List Site
RE: Parsing empty fields XML using perl | Re: Can't get 'content' element to fold correctly in XML::Simple
& XSLT 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
Perl-XML@[...].com
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs
Thread:
Mark Knoop
Jenda Krynicky

Privacy Policy | Email Opt-out | Feedback | Syndication
© ActiveState Software Inc. All rights reserved