http://www.perlmonks.org?node_id=1007959


in reply to XML::Simple parsing help

Perhaps the following will assist you:

use strict; use warnings; use XML::Simple; my $xml = <<'END'; <inventors type="array"> <inventor> <city>Aston Clinton</city> <name>Andy Barth</name> <number type="integer">1</number> <country>GB</country> <upper-name>ANDY BARTH</upper-name> </inventor> <inventor> <city>Aylesbury</city> <name>Daniele Dall'Acqua</name> <number type="integer">2</number> <country>GB</country> <upper-name>DANIELE DALL'ACQUA</upper-name> </inventor> <inventor> <city>Calne</city> <name>Nigel Drew</name> <number type="integer">3</number> <country>GB</country> <upper-name>NIGEL DREW</upper-name> </inventor> </inventors> END my $xml_to_hash = XMLin( $xml, ForceArray => 1, KeyAttr => {}, ); for my $inventor ( @{ $xml_to_hash->{inventor} } ) { if ( $inventor->{number}[0]{content} == 1 ) { while ( my ( $key, $value ) = each %{$inventor} ) { print "$key => @$value[0]\n" if $key ne 'number'; } } }

Output:

country => GB city => Aston Clinton upper-name => ANDY BARTH name => Andy Barth

If you review the Dumper output of $xml_to_hash, you'll see the dereferencing used above to get the data. Remember:

[ ] = array { } = hash