Flat file to XML

Discussion in 'XML' started by Rob, Apr 6, 2007.

  1. Rob

    Rob Guest

    Anyone know of a simple way to convert a UNIX flat file (tab
    delimited)with about 20 fields(one header) to a xml file. What I want to
    do it to be able to open the file in a spreadsheet easily.

    thanks,
    -Rob
     
    Rob, Apr 6, 2007
    #1
    1. Advertising

  2. Rob wrote:

    > Anyone know of a simple way to convert a UNIX flat file (tab
    > delimited)with about 20 fields(one header) to a xml file. What I want to
    > do it to be able to open the file in a spreadsheet easily.


    With XSLT 2.0 (as implemented by Saxon from <http://www.saxonica.com>)
    it should be easy to implement the conversion, using the unparsed-text
    function and the tokenize function.

    --

    Martin Honnen
    http://JavaScript.FAQTs.com/
     
    Martin Honnen, Apr 6, 2007
    #2
    1. Advertising

  3. Rob wrote:
    > What I want to do it to be able to open the file in a spreadsheet easily.


    Most spreadsheet programs can already import CSV or TSV files; I'm not
    sure why you're going through XML for this specific application.

    If you really want to implement your own TSV-to-XML parser, I'd suggest
    you consider swiping and adapting the simple CSV parser shown at
    http://xerces.apache.org/xerces2-j/xni-config.html#csv-parser-config
    There's an example of how to invoke it as a SAXParser earlier on that page.

    --
    Joe Kesselman / Beware the fury of a patient man. -- John Dryden
     
    Joseph Kesselman, Apr 6, 2007
    #3
  4. Rob wrote:
    >
    > Anyone know of a simple way to convert a UNIX flat file (tab
    > delimited)with about 20 fields(one header) to a xml file. What I want to
    > do it to be able to open the file in a spreadsheet easily.
    >
    > thanks,
    > -Rob


    Look here :
    http://reflex.gforge.inria.fr/tutorial.html#N801B3C

    With XPath, you can match your input and create your custom XML structure.
    If the "line reader" is not enough, you can also use a regular
    expression (read the other examples)

    --
    Cordialement,

    ///
    (. .)
    --------ooO--(_)--Ooo--------
    | Philippe Poulard |
    -----------------------------
    http://reflex.gforge.inria.fr/
    Have the RefleX !
     
    Philippe Poulard, Apr 6, 2007
    #4
  5. Rob

    Rob Guest

    Ok thanks for your quick responses. I want this so the user does not
    have to perform any imports on the Excel side.
    This below looks promising, but I don't know much about these
    applications. I only know how to create simple shell scripts. I may be
    able to create one that reads in each line and echoes what I need to
    create an xml document. I think I'm going to have to do something like that.

    -Rob



    Philippe Poulard wrote:
    > Rob wrote:
    >>
    >> Anyone know of a simple way to convert a UNIX flat file (tab
    >> delimited)with about 20 fields(one header) to a xml file. What I want
    >> to do it to be able to open the file in a spreadsheet easily.
    >>
    >> thanks,
    >> -Rob

    >
    > Look here :
    > http://reflex.gforge.inria.fr/tutorial.html#N801B3C
    >
    > With XPath, you can match your input and create your custom XML structure.
    > If the "line reader" is not enough, you can also use a regular
    > expression (read the other examples)
    >
     
    Rob, Apr 6, 2007
    #5
  6. Rob <> writes:

    > Ok thanks for your quick responses. I want this so the user does not
    > have to perform any imports on the Excel side.
    > This below looks promising, but I don't know much about these
    > applications. I only know how to create simple shell scripts. I may
    > be able to create one that reads in each line and echoes what I need
    > to create an xml document. I think I'm going to have to do something
    > like that.


    Do you know awk? Here is a small awk command that will output
    something that _may_ be well-formed XML (assuming the first line
    contains field names). Adapt at your will, be careful with encodings
    and quoting in the initial data, plug in your specific tags. And don't
    expect too much.

    awk -F'\t' '
    BEGIN { printf("<file>\n"); }
    NR==1 {
    for ( i=1 ; i<=NF ; i++ )
    name = $i;
    }
    NR>1 {
    for ( i=1 ; i<=NF ; i++ )
    printf("<%s>%s</%s>\n",name,$i,name);
    }
    END { printf("</file>\n"); }
    ' yourfile.csv

    -- Alain.
     
    Alain Ketterlin, Apr 6, 2007
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. BlackSnail
    Replies:
    1
    Views:
    434
    Christopher Boomer
    Aug 6, 2003
  2. Replies:
    0
    Views:
    588
  3. Homer
    Replies:
    2
    Views:
    847
    Homer
    May 29, 2006
  4. R. P.
    Replies:
    3
    Views:
    8,340
    Joe Kesselman
    Jun 22, 2006
  5. c676228
    Replies:
    3
    Views:
    377
    Mike Brind [MVP]
    Jul 17, 2008
Loading...

Share This Page