jhoop has asked for the wisdom of the Perl Monks concerning the following question:
Hello, I have some XML:
... <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> ...
And I need to access the name of the inventor with Number=1. Using XML::Simple as follows
my $xml_to_hash = XMLin($xml_file, #ForceArray => 0, #KeyAttr => {}, );
I get this output:
$VAR1 = { 'inventors' => { 'inventor' => { 'Nigel Drew' => { 'country' => 'GB', 'city' => 'Calne', 'number' => { 'content' => '3', 'type' => 'integer' }, 'upper-name' => 'NIGEL DREW' }, 'Daniele Dall\'Acqua' => { 'country' => 'GB', 'city' => 'Aylesbury', 'number' => { 'content' => '2', 'type' => 'integer' }, 'upper-name' => 'DANIELE DALL\'ACQUA' }, 'Andy Barth' => { 'country' => 'GB', 'city' => 'Aston Clinton', 'number' => { 'content' => '1', 'type' => 'integer' }, 'upper-name' => 'ANDY BARTH' } }, 'type' => 'array' },
And with ForceArray enabled:
$VAR1 = { 'inventors' => [ { 'inventor' => [ { 'country' => ['GB'], 'city' => ['Aston Clinton'], 'upper-name' => ['ANDY BARTH'], 'number' => [{'content' => '1', 'type' => 'integer'}], 'name' => ['Andy Barth'] }, { 'country' => ['GB'], 'city' => ['Aylesbury'], 'upper-name' => ['DANIELE DALL\'ACQUA'], 'number' => [{'content' => '2', 'type' => 'integer'}], 'name' => ['Daniele Dall\'Acqua'] }, { 'country' => ['GB'], 'city' => ['Calne'], 'upper-name' => ['NIGEL DREW'], 'number' => [{'content' => '3', 'type' => 'integer'}], 'name' => ['Nigel Drew'] } ], 'type' => 'array' } ], ...
In attempting to access Inventor #1 (with ForceArray disabled) I tried the following:
foreach my $inventors(%{$xml_to_hash->{inventors}}){ if ($inventors->{inventor}->{number}->{content} == 1){ print $inventors->{inventor}; } }
to which I get error: "Can't use string ("inventor") as a HASH ref.." I also tried:
foreach my $inventor(%{$xml_to_hash->{inventors}->{inventor}}){ if ($inventor->{number}->{content} == 1){ print $inventor; } }
Which gives "Can't use string ("Nigel Drew") as a HASH ref.."
I'm not sure the output with ForceArray enabled is simplifying anything, and not sure how to begin accessing that structure.. Maybe I should be trying to use KeyAttr to organize the inventors under the <number> field, but my attempts have proved fruitless.. I've been using the manual and some info here: http://interoperating.info/courses/perl4data/node/26 .. Any help much appreciated. Thanks for your time.
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: XML::Simple parsing help
by Kenosis (Priest) on Dec 09, 2012 at 07:54 UTC | |
by bitingduck (Chaplain) on Dec 09, 2012 at 08:15 UTC | |
by Kenosis (Priest) on Dec 09, 2012 at 08:24 UTC | |
by bitingduck (Chaplain) on Dec 09, 2012 at 08:29 UTC | |
by jhoop (Acolyte) on Dec 09, 2012 at 16:43 UTC | |
by jhoop (Acolyte) on Dec 09, 2012 at 16:41 UTC | |
by Kenosis (Priest) on Dec 09, 2012 at 22:56 UTC | |
Re: XML::Simple parsing help
by Jenda (Abbot) on Dec 09, 2012 at 13:41 UTC | |
by jhoop (Acolyte) on Dec 09, 2012 at 16:28 UTC | |
by Anonymous Monk on Dec 10, 2012 at 03:14 UTC | |
Re: XML::Simple parsing help
by karlgoethebier (Abbot) on Dec 09, 2012 at 15:02 UTC |