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.


In reply to XML::Simple parsing help by jhoop

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.