Solved

From XML to Comma separated format

Posted on 2004-09-27
8
178 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Email validation in proper way is  very important validation required in any web pages. This code is self explainable except that Regular Expression which I used for pattern matching. I originally published as a thread on my website : http://www…
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…
Six Sigma Control Plans

728 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