From XML to Comma separated format

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

sunnybradAsked:
Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

x
 
davorgConnect With a Mentor Commented:
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
 
sstoukCommented:
# 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
 
holliCommented:
normally i prefer xslt for such a task.
but if you insist using perl...please show a sample of your xml.
0
Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

 
vi_srikanthCommented:
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
 
davorgCommented:
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
 
holliCommented:
yeah, you better write a shell script instead if using perl4
0
All Courses

From novice to tech pro — start learning today.