Flat file to XML

R

Rob

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
 
M

Martin Honnen

Rob said:
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.
 
J

Joseph Kesselman

Rob said:
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.
 
P

Philippe Poulard

Rob said:
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 !
 
R

Rob

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
 
A

Alain Ketterlin

Rob said:
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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top