Solved

From XML to Comma separated format

Posted on 2004-09-27
8
174 Views
Last Modified: 2010-03-05
Hi all:

I have perl, version 4.0 on Solaris. How do you go about parsing an XML document and creating a  file in a comma separated format.

I want to know the big picture here. Also small examples with code will be very helpful.

Best Regards

-sunnybrad

0
Comment
Question by:sunnybrad
8 Comments
 
LVL 6

Expert Comment

by:sstouk
ID: 12165225
# I use XML::Simple to put it into a hash:

Use XML::Simple
my $hash_ref= XMLin("$XmlConfigFile");
my %Config = undef;
%Config = %$hash_ref;

# Now I have a hash %Config that represents the structure of the XML file with keys and values.
# We can go though the keys and save it to one or multiple text files, separating values by commas or any other character.
# We have full control over the structure of the resulting text file.
0
 
LVL 6

Expert Comment

by:holli
ID: 12167034
normally i prefer xslt for such a task.
but if you insist using perl...please show a sample of your xml.
0
 
LVL 4

Expert Comment

by:vi_srikanth
ID: 12177182
If the XML has got only a few types of tags(for eg., the whole XML file has got some 10 tags that are repeated thru out the document) then, it is better to use the regular expression.
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 8

Expert Comment

by:davorg
ID: 12187889
It's hard to give a detailed answer without knowing the structure of your XML file, but you might be able to get some ideas from this:

#!/usr/bin/perl
                                                                               
use strict;
use warnings;
use XML::XPath;
                                                                               
my $xp = XML::XPath->new(ioref => \*DATA);
                                                                               
my $head;
foreach my $r ($xp->findnodes('//record')) {
  my @fields;
  my @head;
  foreach my $f ($r->findnodes('./*')) {
    push @fields, $f->findvalue('.');
    push @head, $f->getName unless $head;
  }
  unless ($head) {
    print join ',', map { qq("$_") } @head;
    print "\n";
    $head++;
  }
  print join ',', map { qq("$_") } @fields;
  print "\n";
}
                                                                               
__END__
<data>
  <record>
    <field1>foo</field1>
    <field2>bar</field2>
    <field3>baz</field3>
  </record>
  <record>
    <field1>foo</field1>
    <field2>bar</field2>
    <field3>baz</field3>
  </record>
</data>
0
 
LVL 8

Accepted Solution

by:
davorg earned 500 total points
ID: 12187917
Oh wait. I've just noticed the part where you say "I have perl, version 4.0".

None of the solutions given will work with Perl 4. Perl 4 doesn't have support for modules and all of the solutions given (including mine) rely on external modules to do the XML parsing. This is very sensible as you certainly don't want to get into writing your own XML parsing routines.

The first version of Perl 5 was released ten years ago. Perl 4 is dead dead dead. It really shouldn't be used any more.

Your first priority should be to upgrade your version of Perl, your sysadmin. your manager or your job as appropriate!

Dave...
0
 
LVL 6

Expert Comment

by:holli
ID: 12191932
yeah, you better write a shell script instead if using perl4
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

I've just discovered very important differences between Windows an Unix formats in Perl,at least 5.xx.. MOST IMPORTANT: Use Unix file format while saving Your script. otherwise it will have ^M s or smth likely weird in the EOL, Then DO NOT use m…
Checking the Alert Log in AWS RDS Oracle can be a pain through their user interface.  I made a script to download the Alert Log, look for errors, and email me the trace files.  In this article I'll describe what I did and share my script.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Hi friends,  in this video  I'll show you how new windows 10 user can learn the using of windows 10. Thank you.

910 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now