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

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
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":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others about the Monastery: (4)
As of 2024-04-18 08:15 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found