XML to CSV Conversion

Ask a Question related to Perl / CGI, Design and Development.

  1. #1

    Default XML to CSV Conversion

    Hi All,

    Does anyone have a perl script handy to show me how to convert an XML
    document to CSV format ?

    Thanks in advance.
    Ravi Guest

  2. Similar Questions and Discussions

    1. Conversion Mac to PC
      My dept. is switching from mac to pc for networking reasons. How can I convert the pagemaker 6.5 documents on the mac to indesign documents on the...
    2. RGB Conversion
      Help! We're on a mac and changed our text in Illustrator to RGB 0/0/102, saved and closed the file. When we reopened the file, the RGB percentages...
    3. dxf conversion
      hello, I have been using AutoCAD LT to create .dxf files which are then imported into FH MX. However, I have noticed that the size of features...
    4. [PHP] Mp3 Conversion, using PHP
      I'm wondering if there's any crazy dude out there that would like to join me in making a class for PHP to convert Mp3's to Mp3Pro. Have you...
    5. A bit OT: conversion mp3 to wav
      In my deb testing installation until recently I had been using a widely known procedure to convert mp3 files into wav to roast CDs. in a nutshell...
  3. #2

    Default Re: XML to CSV Conversion

    On 18 Aug 2003 06:53:19 -0700
    [email]anamalay@hotmail.com[/email] (Ravi) wrote:
    > Does anyone have a perl script handy to show me how to convert an
    > XML document to CSV format ?
    Someone has gone to the trouble of creating the XML::CSV module just
    for this purpose.

    HTH

    Jim
    James Willmore Guest

  4. #3

    Default Re: XML to CSV Conversion

    [email]anamalay@hotmail.com[/email] (Ravi) wrote in message news:<2c6dd813.0308180553.382c295b@posting.google. com>...
    > Hi All,
    >
    > Does anyone have a perl script handy to show me how to convert an XML
    > document to CSV format ?
    >
    Here is a way, using XML::XPath:

    [trwww@waveright perl]# cat xml2csv.pl
    #!/usr/bin/perl
    use warnings;
    use strict;
    use XML::XPath;

    my($xp) = XML::XPath->new( join('', <DATA>) );
    my(@records) = $xp->findnodes( '/records/record' );
    my($firstTime) = 0;

    foreach my $record ( @records ) {
    my(@fields) = $xp->find( './child::*', $record )->get_nodelist();
    unless ( $firstTime++ ) {
    print( join( ',', map { $_->getName() } @fields ), "\n");
    }

    print( join( ',', map { $_->string_value() } @fields ), "\n");

    }

    __DATA__
    <records>
    <record>
    <id>1</id>
    <name>George Foo</name>
    <phone>555.666.7777</phone>
    </record>
    <record>
    <id>2</id>
    <name>Betty Bar</name>
    <phone>666.777.8888</phone>
    </record>
    </records>

    [trwww@waveright perl]# perl xml2csv.pl
    id,name,phone
    1,George Foo,555.666.7777
    2,Betty Bar,666.777.8888

    If your data has commas in it, then you can run $_->string_value()
    through a filter that escapes the comma.

    enjoy,

    Todd W.
    trwww Guest

  5. #4

    Default Re: XML to CSV Conversion


    "James Willmore" <jwillmore@cyberia.com> wrote in message
    news:20030818155810.44fe12fa.jwillmore@cyberia.com ...
    > On 18 Aug 2003 06:53:19 -0700
    > [email]anamalay@hotmail.com[/email] (Ravi) wrote:
    > > Does anyone have a perl script handy to show me how to convert an
    > > XML document to CSV format ?
    >
    > Someone has gone to the trouble of creating the XML::CSV module just
    > for this purpose.
    >
    The shorthand description of XML::CSV is: "Perl extension converting CSV
    files to XML". I couldn't find anything in the documentation that suggests
    that this module goes the other way around.


    James E Keenan Guest

  6. #5

    Default Re: XML to CSV Conversion

    "James E Keenan" <jkeen@concentric.net> wrote in message news:<bhrlrt$3eu@dispatch.concentric.net>...
    > "James Willmore" <jwillmore@cyberia.com> wrote in message
    > news:20030818155810.44fe12fa.jwillmore@cyberia.com ...
    > > On 18 Aug 2003 06:53:19 -0700
    > > [email]anamalay@hotmail.com[/email] (Ravi) wrote:
    > > > Does anyone have a perl script handy to show me how to convert an
    > > > XML document to CSV format ?
    > >
    > > Someone has gone to the trouble of creating the XML::CSV module just
    > > for this purpose.
    > >
    >
    > The shorthand description of XML::CSV is: "Perl extension converting CSV
    > files to XML". I couldn't find anything in the documentation that suggests
    > that this module goes the other way around.
    You appear to be correct. How about:
    AnyData::Format::XML

    -or- you could perform the same search I used to find this - go to
    [url]http://search.cpan.org/[/url]

    There are several XML modules listed there - some use SAX, others
    Expat, others are pure Perl. I played around with XML, but have had
    no real need to use it. I'd like to, because XML files can be used in
    some many different ways - just have not found them time.

    Or you could write your own - which I think you wanted to avoid doing.

    HTH and sorry I can't be of more help - maybe someone else who uses
    XML regularlly can chime in.

    Jim
    James Willmore Guest

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139