vaibhav.shukla has asked for the wisdom of the Perl Monks concerning the following question:

Hi Perl monks team,

I am trying to Parse multiple XML and Store the values into hashmap as Key/Value Pair.

I have tried to Parse multiple XML and Store the values into array as my certain fields has duplicate values so I want the unique value of it and store its corresponding values.

Does anyone can suggest me ideas?

I am unable to initiate it as I don't know how to store key/value pair in Hashmap. and sample of the XML is

<?xml version="1.0" encoding="utf-8"?> <!-- Configuration for the aero product --> <product name="aero" xsi:noNamespaceSchemaLocation="product.xsd" xmlns +:xsi="http://www.w3.org/2001/XMLSchema-instance"> <productName>Airtime</productName> <licenseName>Airtime_Tool</licenseName> <externalProductIdentifier>10</externalProductIdentifier> <baseCode>AT</baseCode> <released>true</released> <releasePlatforms> <platform>all</platform> </releasePlatforms> <dependsOn> <componentDep name="air"></componentDep> <componentDep name="air"></componentDep> <componentDep name="air"></componentDep> </dependsOn> <requiredProducts> <productDep name="air"></productDep> </requiredProducts> <releaseTypeAndDelivery> <releaseTD releaseType="PVersion" releaseDelivery="DVD"/> <releaseTD releaseType="PVersion" releaseDelivery="Web"/> <releaseTD releaseType="SVersion" releaseDelivery="Web"/> </releaseTypeAndDelivery> </product>

and I only need

<productName>Airtime</productName> <licenseName>Airtime_Tool</licenseName> <externalProductIdentifier>10</externalProductIdentifier> <baseCode>AT</baseCode> <product name =aero>

and I want to make licenseName as key and rest as a Values of it. I have tried it as

my $xmldata= XMLin($filename); foreach my $licenseName (keys %{$xmldata->{licenseName}}) { print $licenseName . ' is ' . $xmldata->{basecode}->{$basecode +} . $xmldata->{externalProductIdentifier}->{$externalProductIdentifie +r} . "\n"; }
Thanks!!!

Replies are listed 'Best First'.
Re: Parsing multiple XML and Store the values into hashmap as Key/Value Pair
by Loops (Curate) on Jul 17, 2013 at 23:42 UTC

    Hi there, this should get you going:

    use strict; use warnings; use XML::Hash; use Data::Dumper; my %data; open my $xmldata, '<', 'file.xml' or die $!; for my $product (values XML::Hash->new()->fromXMLStringtoHash($xmldata +)) { my ($product, $license, $id, $code, $name) = ( $product->{productName}->{text}, $product->{licenseName}->{text}, $product->{externalProductIdentifier}->{text}, $product->{baseCode}->{text}, $product->{name}); $data{$license} = [ $product, $id, $code, $name ]; } print Dumper \%data/

    Output when run against your example xml:

    $VAR1 = { 'Airtime_Tool' => [ 'Airtime', '10', 'AT', 'aero' ] };

    *Updated the code to store into a hash map