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

Hey Monks

Was wondering if anyone could help me work out how to parse the output I have got from loading my XML file into XML::Simple.

Here is my program so far...

use XML::Simple; my $filename="file.xml"; my $xml = XMLin($filename); ## produces the output below print Dumper($xml); ## this bit isn't right because the xml isnt in ## an array but I dont know how to reference ## the products and put them into an array my @products = @{$xml->{product}}; foreach my $item (@products) { Print "Department: $item->{department}\n"; Print "Manufacturer: $item->{manufacturer}\n"; }

How can I loop through each of the products and get the values for each of the elements? I need to insert these into a database...

This is the structure of my XML file, as output by Dumper.

$VAR1 = { 'product' => { 'ORANGE MOTO V545' => { 'department' => 'Computing & Phones', 'startdate' => '1900-01-01 00:00:00.0', 'format' => {}, 'manufacturer' => 'ORANGE' }, 'MORPHY RICHARDS 44411' => { 'department' => 'Kitchen & Home', 'startdate' => '1900-01-01 00:00:00.0', 'format' => {}, 'manufacturer' => 'MORPHY RICHARDS' }, };

Thanks so much.

Learning without thought is labor lost; thought without learning is perilous. - Confucius
WebChalkboard.com | For the love of art...

Replies are listed 'Best First'.
Re: Parsing XML Simple Output
by mirod (Canon) on Apr 15, 2005 at 16:33 UTC

    You could just loop through the hash while( my( $key, $product)= each %{$xml->{product}) { ... }, or you could change the options for XMLin. I guess ORANGE MOTO V545 is in an attribute that's considered a key by XMLin ( name, key or id by default). Look at the KeyAttr option to change this.

      Thanks! I think that's put me on the way to working this one out ;)

      Learning without thought is labor lost; thought without learning is perilous. - Confucius
      WebChalkboard.com | For the love of art...