Professional Web Applications Themes
  1. #1

    Default XML to CSV Conversion

    Hi All,

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

    Thanks in advance.
    Ravi Guest

  2. #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 doent to CSV format ?
    Someone has gone to the trouble of creating the XML::CSV module just
    for this purpose.

    HTH

    Jim
    James Willmore Guest

  3. #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
    > doent 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

  4. #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 doent 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 doentation that suggests
    that this module goes the other way around.


    James E Keenan Guest

  5. #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 doent 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 doentation 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

Similar Threads

  1. Conversion Mac to PC
    By Linda_Aber@adobeforums.com in forum Adobe Illustrator Windows
    Replies: 2
    Last Post: June 15th, 08:28 PM
  2. RGB Conversion
    By Carolyn_Gerard@adobeforums.com in forum Adobe Illustrator Macintosh
    Replies: 3
    Last Post: April 25th, 10:05 PM
  3. dxf conversion
    By ersc14 webforumsuser@macromedia.com in forum Macromedia Freehand
    Replies: 1
    Last Post: November 17th, 11:29 PM
  4. conversion from mid to Wav
    By David Scrève in forum Mac Programming
    Replies: 0
    Last Post: August 17th, 10:02 PM
  5. A bit OT: conversion mp3 to wav
    By vdemart@supereva.it in forum Debian
    Replies: 2
    Last Post: July 8th, 08:10 AM

Bookmarks

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