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
Solved

From XML to Comma separated format

Posted on 2004-09-27
8
176 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
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

 
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

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Perl script to parse log and delete the file 17 176
Perl 101 11 81
Perl string replace for refred url 9 63
Any syntax error for this clone.plscript 6 143
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…
Many time we need to work with multiple files all together. If its windows system then we can use some GUI based editor to accomplish our task. But what if you are on putty or have only CLI(Command Line Interface) as an option to  edit your files. I…
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…

791 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