Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Clobbered keys in XML::Simple

by Apterigo (Scribe)
on Aug 29, 2002 at 16:00 UTC ( #193808=perlquestion: print w/replies, xml ) Need Help??

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

Hello Monks,

I am using the XML::Simple module to parse an XML file and input some fields into a database. Everything works well, except there is a section such as:

<DataContent> <head> <meta name="APU:IndustryCode" content="Banking / finance" /> <meta name="APU:IndustryCode" content="Joint ventures / investment" />

If I write code such as:

my $xs1 = XML::Simple->new(); $doc = $xs1->XMLin('./foo.xml'); $industrycode1 = $doc->{DataContent}->{head}->{meta}->{"APU:IndustryCo +de"}->{content};
to read this, it only returns the second value (ie. Joint ventures / investment) and not the first value. I cannot find an easy to get both values. Any ideas?

Apterigo

Replies are listed 'Best First'.
Clobbered keys in XML::Simple
by Rhose (Priest) on Aug 29, 2002 at 17:45 UTC
    What is happening here is that 'name' is one of the default key attributes for XML::Simple. (The defaults are 'name', 'key', and 'id'.) By changing this (with the keyattr option), I think you can get the desired results.

    Here is a quick script I threw together which should demonstrate the differences. I hope it helps.

    #!/usr/bin/perl -w use strict; use Data::Dumper; use XML::Simple; my $gXML; my $gXMLDoc; my $gXMLString; $/ = undef; $gXMLString = <DATA>; $gXML = XML::Simple->new(); $gXMLDoc = $gXML->XMLin($gXMLString); print 'Before:',"\n",'-' x 40,"\n",Dumper($gXMLDoc),"\n"; $gXMLDoc = $gXML->XMLin($gXMLString, keyattr => ''); print 'After:',"\n",'-' x 40,"\n",Dumper($gXMLDoc),"\n"; __DATA__ <DataContent> <head> <meta name="APU:IndustryCode" content="Banking / finance" /> <meta name="APU:IndustryCode" content="Joint ventures / investment" /> </head> </DataContent>

    For more information on the options available with XML::Simple (forcearray is another one you will want to make sure you know), check out:

    perldoc XML::Simple

    Note:

    Make sure you look at the data structure dumped with Data::Dumper -- meta is now a two element array.

    Update:

    The following code will iterate through the meta array:

    foreach (0..$#{$gXMLDoc->{head}->{meta}}) { print $gXMLDoc->{head}->{meta}[$_]->{content},"\n"; }
Re: Clobbered keys in XML::Simple
by grantm (Parson) on Aug 29, 2002 at 20:51 UTC

    In addition to the excellent answer from Rhose, I'd direct you to the the XML::Simple FAQ.

    As a rule of thumb, you should always specify explicit values for both the 'forcearray' and 'keyattr' options. Ideally, you would set forcearray to an anonymous array of element names eg:

      forcearray => [ 'meta' ]

    or if that's too hard, set it to 1.

    The best way to use keyattr is to set it to a hash where the keys are the element names and the values are the attribute (or sub-element) that uniquely identifies that type of element eg:

      keyattr => { employee => 'empno' }

    If that doesn't make sense, set keyattr to an empty list:

      keyattr => [ ]

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://193808]
Approved by gryphon
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others chilling in the Monastery: (2)
As of 2022-08-18 20:54 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?