Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

Complex XML hash to csv

by CSharma (Sexton)
on Oct 15, 2015 at 01:34 UTC ( [id://1144924]=perlquestion: print w/replies, xml ) Need Help??

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

Hi PM, I've a xml file & have read into hashes; actually xml is quite complex. Below is the example of single product. I've used XML::Simple to create hash.
I want to extract few values & print it to a csv file. There are many <product> under <products> tag.
link,image_link,name,long,code

To extract 'image_link', I tried this but it prints image_url many time (no of keys). I just want it once.
Could someone plz help?
foreach my $d (keys %{$doc->{'results'}{'result'}{'output'}{'products' +}{'product'}}) { print $doc->{'results'}{'result'}{'output'}{'products'}{'produ +ct'}{'image_link'} . "\n"; }
$VAR1 = { 'xmlns' => 'http://marketlive.com/integration/xmlbean', 'results' => { 'info' => { 'environment' => 'prdv596', 'vmId' => 'iws-currentusa-prdv596-int +0001', 'site' => 'currentusa', 'systemLocale' => 'en_US', 'vmType' => 'production-iws', 'timeStamp' => 'Mon Aug 17 02:35:02 P +DT 2015', 'host' => 'localhost' }, 'result' => { 'summary' => { 'xmlns' => 'http://mar +ketlive.com/integration/xmlbean', 'failed' => '0', 'total' => '15248' }, 'output' => { 'products' => { 'xmlns' = +> 'http://marketlive.com/integration/xmlbean', 'product' + => { + 'xmlns' => 'http://marketlive.com/integration/xmlbean', + 'suppressSwatches' => 'true', + 'dateModified' => '2013-06-05T08:12:59.368-07:00', + 'imagePages' => { + 'imagePage' => { + 'siteImages' => { + 'siteImage' => { + 'de +tail' => '/lillianvernon/products/42642_dt.jpg', + 'al +ternateLarge1' => '/lillianvernon/products/viewlarger/42642_lg.jpg', + 'si +teCode' => 'lillianvernon', + 'th +umb' => '/lillianvernon/products/42642_th.jpg' + } + }, + 'locale' => 'en_US' + } + }, + 'dateActivate' => '2013-06-05T08:12:00.000-07:00', + 'imageTargetPages' => { + 'imageTargetPage' => { + 'siteImageTargets' => +{ + +'siteImageTarget' => [ + + { + + 'zoom1Image' => '/lillianvernon/products/42642 +_dt.jpg', + + 'siteCode' => 'lillianvernon' + + }, + + { + + 'zoom1Image' => '/lillianvernon/products/42642 +_dt.jpg', + + 'siteCode' => 'currentcatalog' + + }, + + { + + 'zoom1Image' => '/lillianvernon/products/42642 +_dt.jpg', + + 'siteCode' => 'currentlabels' + + }, + + { + + 'zoom1Image' => '/lillianvernon/products/42642 +_dt.jpg', + + 'siteCode' => 'colorfulimages' + + } + + ] + }, + 'locale' => 'en_US' + } + }, + 'skus' => { + 'sku' => [ + { + 'attributePages' => { + 'attributePage' => { + 'name +' => 'assignedAttributePage', + 'attr +ibutes' => { + + 'value' => 'Small', + + 'name' => 'Size Family' + + } + } + }, + 'productSkuLinks' => { + 'productSkuLink' => { + 'pr +oductCode' => '42642', + 'de +faultProduct' => 'false' + } + }, + 'dateModified' => '2015-05-11T06:41:50.000-0 +7:00', + 'skuTypeId' => '1', + 'dateActivate' => '2010-12-07T00:00:00.000-0 +8:00', + 'weight' => '0.0', + 'code' => '426426', + 'dateCreated' => '2010-12-07T14:22:24.724-08 +:00', + 'skuOptionLinks' => { + 'skuOptionLink' => { + 'opti +onTypeCode' => 'SIZE', + 'opti +onCode' => '6' + } + }, + 'pk' => '100186', + 'active' => 'false', + 'taxable' => 'true', + 'deleted' => 'false', + 'giftWrap' => 'false', + 'shippingChargeable' => 'true', + 'dateDeactivate' => '2040-12-07T00:00:00.000 +-08:00', + 'inventories' => { + 'inventory' => { + 'pk' => '100 +186', + 'dateModifie +d' => '2010-12-07T14:22:24.724-08:00', + 'inventorySe +tCode' => 'lillianvernon', + 'deleted' => + 'false', + 'minStock' = +> '10', + 'stock' => ' +500', + 'ordinal' => + '0', + 'dateRestock +' => '2011-12-15T00:00:00.000-08:00', + 'code' => '4 +26426', + 'dateCreated +' => '2010-12-07T14:22:24.724-08:00' + } + }, + 'overWeight' => 'false', + 'prices' => { + 'price' => { + 'salePrice' => '158.0 +0', + 'currency' => 'USD', + 'regularPrice' => '15 +8.00', + 'overweightPrice' => +'0.00' + } + } + }, + { + 'attributePages' => { + 'attributePage' => { + 'name +' => 'assignedAttributePage', + 'attr +ibutes' => { + + 'value' => 'Medium', + + 'name' => 'Size Family' + + } + } + }, + 'productSkuLinks' => { + 'productSkuLink' => { + 'pr +oductCode' => '42642', + 'de +faultProduct' => 'false' + } + }, + 'dateModified' => '2015-05-11T06:41:50.000-0 +7:00', + 'skuTypeId' => '1', + 'dateActivate' => '2010-12-07T00:00:00.000-0 +8:00', + 'weight' => '0.0', + 'code' => '4264216', + 'dateCreated' => '2010-12-07T14:26:12.380-08 +:00', + 'skuOptionLinks' => { + 'skuOptionLink' => { + 'opti +onTypeCode' => 'SIZE', + 'opti +onCode' => '16' + } + }, + 'pk' => '100191', + 'active' => 'false', + 'taxable' => 'true', + 'deleted' => 'false', + 'giftWrap' => 'false', + 'shippingChargeable' => 'true', + 'dateDeactivate' => '2040-12-07T00:00:00.000 +-08:00', + 'inventories' => { + 'inventory' => { + 'pk' => '100 +191', + 'dateModifie +d' => '2010-12-07T14:26:12.599-08:00', + 'inventorySe +tCode' => 'lillianvernon', + 'deleted' => + 'false', + 'minStock' = +> '10', + 'stock' => ' +500', + 'ordinal' => + '0', + 'dateRestock +' => '2011-12-15T00:00:00.000-08:00', + 'code' => '4 +264216', + 'dateCreated +' => '2010-12-07T14:26:12.599-08:00' + } + }, + 'overWeight' => 'false', + 'prices' => { + 'price' => { + 'salePrice' => '158.0 +0', + 'currency' => 'USD', + 'regularPrice' => '15 +8.00', + 'overweightPrice' => +'0.00' + } + } + }, + { + 'attributePages' => { + 'attributePage' => { + 'name +' => 'assignedAttributePage', + 'attr +ibutes' => { + + 'value' => 'Small', + + 'name' => 'Size Family' + + } + } + }, + 'productSkuLinks' => { + 'productSkuLink' => { + 'pr +oductCode' => '42642', + 'de +faultProduct' => 'false' + } + }, + 'dateModified' => '2015-05-11T06:41:50.000-0 +7:00', + 'skuTypeId' => '1', + 'dateActivate' => '2010-12-07T00:00:00.000-0 +8:00', + 'weight' => '0.0', + 'code' => '426422', + 'dateCreated' => '2010-12-07T14:15:45.021-08 +:00', + 'skuOptionLinks' => { + 'skuOptionLink' => { + 'opti +onTypeCode' => 'SIZE', + 'opti +onCode' => '2' + } + }, + 'pk' => '100184', + 'active' => 'false', + 'taxable' => 'true', + 'deleted' => 'false', + 'giftWrap' => 'false', + 'shippingChargeable' => 'true', + 'dateDeactivate' => '2040-12-07T00:00:00.000 +-08:00', + 'inventories' => { + 'inventory' => { + 'pk' => '100 +184', + 'dateModifie +d' => '2010-12-07T14:15:45.021-08:00', + 'inventorySe +tCode' => 'lillianvernon', + 'deleted' => + 'false', + 'minStock' = +> '10', + 'stock' => ' +500', + 'ordinal' => + '0', + 'dateRestock +' => '2011-12-15T00:00:00.000-08:00', + 'code' => '4 +26422', + 'dateCreated +' => '2010-12-07T14:15:45.021-08:00' + } + }, + 'overWeight' => 'false', + 'prices' => { + 'price' => { + 'salePrice' => '158.0 +0', + 'currency' => 'USD', + 'regularPrice' => '15 +8.00', + 'overweightPrice' => +'0.00' + } + } + }, + { + 'attributePages' => { + 'attributePage' => { + 'name +' => 'assignedAttributePage', + 'attr +ibutes' => { + + 'value' => 'Small', + + 'name' => 'Size Family' + + } + } + }, + 'productSkuLinks' => { + 'productSkuLink' => { + 'pr +oductCode' => '42642', + 'de +faultProduct' => 'false' + } + }, + 'dateModified' => '2015-05-11T06:41:50.000-0 +7:00', + 'skuTypeId' => '1', + 'dateActivate' => '2010-12-07T00:00:00.000-0 +8:00', + 'weight' => '0.0', + 'code' => '4264210', + 'dateCreated' => '2010-12-07T14:23:44.755-08 +:00', + 'skuOptionLinks' => { + 'skuOptionLink' => { + 'opti +onTypeCode' => 'SIZE', + 'opti +onCode' => '10' + } + }, + 'pk' => '100188', + 'active' => 'false', + 'taxable' => 'true', + 'deleted' => 'false', + 'giftWrap' => 'false', + 'shippingChargeable' => 'true', + 'dateDeactivate' => '2040-12-07T00:00:00.000 +-08:00', + 'inventories' => { + 'inventory' => { + 'pk' => '100 +188', + 'dateModifie +d' => '2010-12-07T14:23:44.755-08:00', + 'inventorySe +tCode' => 'lillianvernon', + 'deleted' => + 'false', + 'minStock' = +> '10', + 'stock' => ' +500', + 'ordinal' => + '0', + 'dateRestock +' => '2011-12-15T00:00:00.000-08:00', + 'code' => '4 +264210', + 'dateCreated +' => '2010-12-07T14:23:44.755-08:00' + } + }, + 'overWeight' => 'false', + 'prices' => { + 'price' => { + 'salePrice' => '158.0 +0', + 'currency' => 'USD', + 'regularPrice' => '15 +8.00', + 'overweightPrice' => +'0.00' + } + } + }, + { + 'attributePages' => { + 'attributePage' => { + 'name +' => 'assignedAttributePage', + 'attr +ibutes' => { + + 'value' => 'Small', + + 'name' => 'Size Family' + + } + } + }, + 'productSkuLinks' => { + 'productSkuLink' => { + 'pr +oductCode' => '42642', + 'de +faultProduct' => 'false' + } + }, + 'dateModified' => '2015-05-11T06:41:50.000-0 +7:00', + 'skuTypeId' => '1', + 'dateActivate' => '2010-12-07T00:00:00.000-0 +8:00', + 'weight' => '0.0', + 'code' => '426424', + 'dateCreated' => '2010-12-07T14:18:11.505-08 +:00', + 'skuOptionLinks' => { + 'skuOptionLink' => { + 'opti +onTypeCode' => 'SIZE', + 'opti +onCode' => '4' + } + }, + 'pk' => '100185', + 'active' => 'false', + 'taxable' => 'true', + 'deleted' => 'false', + 'giftWrap' => 'false', + 'shippingChargeable' => 'true', + 'dateDeactivate' => '2040-12-07T00:00:00.000 +-08:00', + 'inventories' => { + 'inventory' => { + 'pk' => '100 +185', + 'dateModifie +d' => '2010-12-07T14:18:11.505-08:00', + 'inventorySe +tCode' => 'lillianvernon', + 'deleted' => + 'false', + 'minStock' = +> '10', + 'stock' => ' +500', + 'ordinal' => + '0', + 'dateRestock +' => '2011-12-15T00:00:00.000-08:00', + 'code' => '4 +26424', + 'dateCreated +' => '2010-12-07T14:18:11.505-08:00' + } + }, + 'overWeight' => 'false', + 'prices' => { + 'price' => { + 'salePrice' => '158.0 +0', + 'currency' => 'USD', + 'regularPrice' => '15 +8.00', + 'overweightPrice' => +'0.00' + } + } + }, + { + 'attributePages' => { + 'attributePage' => { + 'name +' => 'assignedAttributePage', + 'attr +ibutes' => { + + 'value' => 'Small', + + 'name' => 'Size Family' + + } + } + }, + 'productSkuLinks' => { + 'productSkuLink' => { + 'pr +oductCode' => '42642', + 'de +faultProduct' => 'false' + } + }, + 'dateModified' => '2015-05-11T06:41:50.000-0 +7:00', + 'skuTypeId' => '1', + 'dateActivate' => '2010-12-07T00:00:00.000-0 +8:00', + 'weight' => '0.0', + 'code' => '4264212', + 'dateCreated' => '2010-12-07T14:25:22.927-08 +:00', + 'skuOptionLinks' => { + 'skuOptionLink' => { + 'opti +onTypeCode' => 'SIZE', + 'opti +onCode' => '12' + } + }, + 'pk' => '100189', + 'active' => 'false', + 'taxable' => 'true', + 'deleted' => 'false', + 'giftWrap' => 'false', + 'shippingChargeable' => 'true', + 'dateDeactivate' => '2040-12-07T00:00:00.000 +-08:00', + 'inventories' => { + 'inventory' => { + 'pk' => '100 +189', + 'dateModifie +d' => '2010-12-07T14:25:22.927-08:00', + 'inventorySe +tCode' => 'lillianvernon', + 'deleted' => + 'false', + 'minStock' = +> '10', + 'stock' => ' +500', + 'ordinal' => + '0', + 'dateRestock +' => '2011-12-15T00:00:00.000-08:00', + 'code' => '4 +264212', + 'dateCreated +' => '2010-12-07T14:25:22.927-08:00' + } + }, + 'overWeight' => 'false', + 'prices' => { + 'price' => { + 'salePrice' => '158.0 +0', + 'currency' => 'USD', + 'regularPrice' => '15 +8.00', + 'overweightPrice' => +'0.00' + } + } + }, + { + 'attributePages' => { + 'attributePage' => { + 'name +' => 'assignedAttributePage', + 'attr +ibutes' => { + + 'value' => 'Small', + + 'name' => 'Size Family' + + } + } + }, + 'productSkuLinks' => { + 'productSkuLink' => { + 'pr +oductCode' => '42642', + 'de +faultProduct' => 'false' + } + }, + 'dateModified' => '2015-05-11T06:41:50.000-0 +7:00', + 'skuTypeId' => '1', + 'dateActivate' => '2010-12-07T00:00:00.000-0 +8:00', + 'weight' => '0.0', + 'code' => '4264214', + 'dateCreated' => '2010-12-07T14:25:46.130-08 +:00', + 'skuOptionLinks' => { + 'skuOptionLink' => { + 'opti +onTypeCode' => 'SIZE', + 'opti +onCode' => '14' + } + }, + 'pk' => '100190', + 'active' => 'false', + 'taxable' => 'true', + 'deleted' => 'false', + 'giftWrap' => 'false', + 'shippingChargeable' => 'true', + 'dateDeactivate' => '2040-12-07T00:00:00.000 +-08:00', + 'inventories' => { + 'inventory' => { + 'pk' => '100 +190', + 'dateModifie +d' => '2010-12-07T14:25:46.130-08:00', + 'inventorySe +tCode' => 'lillianvernon', + 'deleted' => + 'false', + 'minStock' = +> '10', + 'stock' => ' +500', + 'ordinal' => + '0', + 'dateRestock +' => '2011-12-15T00:00:00.000-08:00', + 'code' => '4 +264214', + 'dateCreated +' => '2010-12-07T14:25:46.130-08:00' + } + }, + 'overWeight' => 'false', + 'prices' => { + 'price' => { + 'salePrice' => '158.0 +0', + 'currency' => 'USD', + 'regularPrice' => '15 +8.00', + 'overweightPrice' => +'0.00' + } + } + }, + { + 'attributePages' => {

Replies are listed 'Best First'.
Re: Complex XML hash to csv
by Discipulus (Canon) on Oct 15, 2015 at 07:21 UTC
    Hello CSharma,

    only two remarks. First do not use XML::Simple anymore. As stated in the module's documentation it's use is deprecated. Personally i choosed XML::Twig and I'm happy with it. Specially if your xml data is big and complex you'll enjoy the ability of XML::Twig to parse XML using handlers. Might be that your case is few lines of Perl codes and one line of XML::Twig handler.

    Second point the below lines of your codes does not make much sense to me:
    foreach my $d (keys %{$doc->{'results'}{'result'}{'output'}{'products' +}{'product'}}) { print $doc->{'results'}{'result'}{'output'}{'products'}{'produ +ct'}{'image_link'} . "\n"; }
    I read this like "foreach sheep in sheep_group print name_of_every_sheep_in_the_farm"
    Why do you not use $d ?
    Perhaps you intend something like (i admit i have not seen your big hash..)
    foreach my $d (keys %{$doc->{'results'}{'result'}{'output'}{'products' +}{'product'}}) { print $d->{'image_link'} . "\n"; #UPDATE (was: $d{'image_link +'} ) spotted by choroba (thanks) .. i was sure i put }
    HtH
    L*
    There are no rules, there are no thumbs..
    Reinvent the wheel, then learn The Wheel; may be one day you reinvent one of THE WHEELS.
Re: Complex XML hash to csv
by stevieb (Canon) on Oct 15, 2015 at 02:23 UTC

    Please use <code></code> tags around your input data, code and expected output.

    Review Writeup Formatting Tips

    -stevieb

Re: Complex XML hash to csv
by QuillMeantTen (Friar) on Oct 15, 2015 at 09:17 UTC

    also readmore would be nice

Re: Complex XML hash to csv
by poj (Abbot) on Oct 15, 2015 at 16:16 UTC
    Why does image_link not appear in the XML dump you posted ?
    poj
      Could you please attach sample XML from one whole tag data to test the code its difficult to read from dumper hash

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1144924]
Approved by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others imbibing at the Monastery: (7)
As of 2024-04-24 10:45 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found