Advertisement

09.22.2008 at 11:04AM PDT, ID: 23752294 | Points: 500
[x]
Attachment Details

Convert Multiple .XML .GZ Files to .CSV (Pipe Delimited) Daily [Script Adaptation Help]

Asked by thyros in Perl Programming Language, Extensible Markup Language (XML), Scripting Languages

Hi,

I would appreciate some help with converting filename.xml.gz files to filename.csv using some semi-automated process to convert several files.

I have a small perl code snippet attached which I am currently using to manually convert one xml file at a time to .csv (pipe delimited).

The script checks for a file called 'tags_to_match.txt" which includes a list of xml attribute headers that I want to extract, i.e.  

<number>12</number><text>text</text><general>something else</general>

I would write out as follows in the tags_to_match.txt file.

number
text
general

(one per line for each piece of information to extract).

Then in command prompt I would run the following;

shell>  perl xml2csv.pl < myinput.xml > myoutput.csv

This would take the existing xml file and convert it to pipe delimited csv and output according to the name above.  This works fine but I need to extend the functionality further.  Right now, the output would look something like this;

12|text|something else

However, I need to include the field headers at the top of the file so that the output looks like this;

number|text|general
12|text|something else
48|more text|other general stuff
..

Lastly, I need to process a list of files (about 12 of them on a daily basis) so it becomes quite tedious to write out each command individually, so hopefully this perl snippet can be adapted to look in another file which contains a list of .xml.gz filenames to convert (one per line as I have with tags_to_match.txt).  From there I could setup a crontab to run this one perl script which would then process those multiple files.  The output csv files can be the same name as the input xml files with the difference of the file extension.
Start Free Trial
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
#!/usr/bin/perl -w
use strict;
 
# little script for thyros, Expert-Exchange.com Q_22833778.html
# see http://www.experts-exchange.com/Q_22833778.html
 
open TAGS, "tags_to_match.txt" or die("ERROR: tags file 'tags_to_match.txt' not found");
my $all_tags = do { local $/; <TAGS> };
my @tags = split(/\n/,$all_tags);
 
my $merge_cat_path_count = 0;
$/ = "</item_data>";
while (<>) {
s/\|//g;
 
 
      die("ERROR: pipeline '|' character found in data!") if /\|/;
 
      foreach my $tag (@tags) {
            my $tmpTag = $tag;  # work on a copy
            my $count = 1;
            $count = $1 if $tmpTag =~ s/(\d+)$//;
            print "$1" if m!(?:<$tmpTag>([^<]*)</$tmpTag>.*?){$count}!ms;            # do not set /g modifier!
            print "|";      
      }
      print "\n";
}
[+][-]09.22.2008 at 11:30AM PDT, ID: 22542574

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 30-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]09.22.2008 at 11:30AM PDT, ID: 22542577

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 30-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]09.23.2008 at 05:22AM PDT, ID: 22548447

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 30-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]09.23.2008 at 07:42AM PDT, ID: 22549936

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 30-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]09.24.2008 at 06:35AM PDT, ID: 22559512

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 30-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]09.24.2008 at 06:43AM PDT, ID: 22559582

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 30-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]09.25.2008 at 05:45AM PDT, ID: 22568418

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 30-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]09.25.2008 at 05:54AM PDT, ID: 22568489

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 30-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]09.25.2008 at 06:22AM PDT, ID: 22568765

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 30-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]09.25.2008 at 09:31AM PDT, ID: 22571158

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 30-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]09.25.2008 at 09:38AM PDT, ID: 22571265

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 30-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]09.25.2008 at 11:23AM PDT, ID: 22572358

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 30-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]09.28.2008 at 09:48AM PDT, ID: 22591746

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 30-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]09.28.2008 at 02:34PM PDT, ID: 22592658

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 30-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]09.30.2008 at 04:23AM PDT, ID: 22604262

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 30-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]09.30.2008 at 07:53AM PDT, ID: 22605919

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 30-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]11.18.2008 at 05:37AM PST, ID: 22984329

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 30-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]11.18.2008 at 08:32AM PST, ID: 22986082

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 30-day free trial to view this Expert Comment or ask the Experts your question.

 
 
Loading Advertisement...
20081112-EE-VQP-44 / EE_QW_2_20070628