Hi Everyone -

I've been sitting here for 4 hours and can't figure out the logic to do this. I am parsing data from an XML file and need to output to a CSV. I can parse the data just fine, I just can't figure out how to write it out and then parse it to a csv file that would ultimately look like the desired output below. I certainly appreciate the help. I've tried arrays and hashes and got nowhere.

Desired output

"sku","File 1", "File 2", "File 6", "File 7"
"00123","www.site.com/path/to/file/file.pdf","www.site.com/path/to/file/file.pdf",,
"00124","www.site.com/path/to/file/file.pdf",,"www.site.com/path/to/file/file.pdf",
"00125",,"www.site.com/path/to/file/file.pdf",,"www.site.com/path/to/file/file.pdf"

Perl Script

use 5.010; use strict; use warnings; use utf8; use XML::LibXML; my $source_file = 'C:\Users\User\Files\perl\data\example\example.xml'; my $dom = XML::LibXML->load_xml(location => $source_file); foreach my $part ($dom->findnodes('/products/product')) { my $sku = $part->findnodes('sku'); my $download_item_name; my $download_item_name_url; foreach my $downloads($part->findnodes('downloads')){ foreach my $download_group($downloads->findnodes('group')){ foreach my $downloads_group_items($download_group->findnodes('gr +oup_items')){ foreach my $download_item($downloads_group_items->findnodes('i +tem')){ $download_item_name = $download_item->findnodes('name'); $download_item_name_url = $download_item->findnodes('url'); print $sku . " -> " . "->" . $download_item_name . "->" . $d +ownload_item_name_url . "\n"; } } } } }

XML Data

<products> <product> <sku>00123</sku> <downloads> <group> <group_items> <item> <name>File 1</name> <url>www.site.com/path/to/file/file.pdf</url> </item> <item> <name>File 2</name> <url>www.site.com/path/to/file/file.pdf</url> </item> </group_items> </group> </downloads> </product> <product> <sku>00124</sku> <downloads> <group> <group_items> <item> <name>File 1</name> <url>www.site.com/path/to/file/file.pdf</url> </item> <item> <name>File 6</name> <url>www.site.com/path/to/file/file.pdf</url> </item> </group_items> </group> </downloads> </product> <product> <sku>00125</sku> <downloads> <group> <group_items> <item> <name>File 7</name> <url>www.site.com/path/to/file/file.pdf</url> </item> <item> <name>File 2</name> <url>www.site.com/path/to/file/file.pdf</url> </item> </group_items> </group> </downloads> </product> </products>

In reply to Logic Help - Output to csv by audioboxer

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.