Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

Re: Accessing attributes in XML::Simple

by gube (Parson)
on Dec 31, 2009 at 20:33 UTC ( #815122=note: print w/ replies, xml ) Need Help??


in reply to Accessing attributes in XML::Simple

Hi,

You can do in XML Simple itself by passing KeyAttr => 1. Hope using for loop you can take the value of id.

#!/usr/bin/perl use strict; use warnings; use XML::Simple; use Data::Dumper; my $xml_simple = XML::Simple->new( KeepRoot => 1, KeyAttr => 1, For +ceArray => 1 ); my $response_hash = eval{ $xml_simple->XMLin( "cars.xml" ) }; print Dumper($response_hash); Output : $VAR1 = { 'root' => [ { 'unsold' => [ { 'car' => [ { 'model' => [ 'Hyundai' ], 'mileage' => [ '20000' ] }, { 'model' => [ 'Jeep' ], 'mileage' => [ '10000' ] } ] } ], 'footnotes' => [ { 'footnote' => [ { 'content' => 'F +1 text', 'id' => 'F1' }, { 'content' => 'F +2 text', 'id' => 'F2' } ] } ] } ] };


Comment on Re: Accessing attributes in XML::Simple
Download Code
Replies are listed 'Best First'.
Re^2: Accessing attributes in XML::Simple
by ikegami (Pope) on Dec 31, 2009 at 20:39 UTC

    KeyAttr => 1 is wrong. The value should be an array ref or a hash ref. In this case, you want KeyAttr => []

    ForceArray => 1 is overkill. ForceArray => [qw( car footnote )] would be more appropriate

      You are right ikegami. If array ref i totally agree to you.

      Thanks,

      Gubs

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://815122]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (8)
As of 2015-07-28 07:06 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (252 votes), past polls