Solved

From XML to Comma separated format

Posted on 2004-09-27
8
173 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
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

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…
I have been pestered over the years to produce and distribute regular data extracts, and often the request have explicitly requested the data be emailed as an Excel attachement; specifically Excel, as it appears: CSV files confuse (no Red or Green h…
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…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.

708 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

16 Experts available now in Live!

Get 1:1 Help Now