Python & XML
Jerome Alet
alet at unice.fr
Wed Feb 14 05:07:44 EST 2001
Chris Richard Adams wrote:
> I have a tab seperated file that I would like to parse and store in XML
> format.
>
> line1: 12:00:00 bob nassl.pdf 123.123.123.123
>
> would like to create an xml file from this text file in the form
>
> <upload>
> <time>12:00:00</time>
> <user>bob</user>
> <file>nassl.pdf</file>
> <ip>123.123.123.123</ip>
> </upload>
I suggest you to download my GPLed jaxml module from:
http://cortex.unice.fr/~jerome/jaxml-2.00beta1.tar.gz
then:
--- CUT ---
import jaxml
yourfile = open("inputfile.txt")
doc = jaxml.XML_document()
for line in yourfile.readlines() :
time, name, file, ip = line.split("\t") # same as Paul
doc._push()
doc.upload().time(time).user(name).file(file).ip(ip)
doc._pop()
doc._output("outputfile.xml") # or: print doc
--- CUT ---
will produce exactly what you want, and is able of much more, without
boring you with the indentation and closing tags if you want to have a
human readable XML document: formatting is done automagically. This
module also includes templating facilities and other goodies.
You won't find something easier to use ! (or else just tell me)
don't be afraid of the "beta1", it should be read as "final" ;-)
Comments are very welcome.
--
Jerome Alet - alet at unice.fr - http://cortex.unice.fr/~jerome
Fac de Medecine de Nice http://wwwmed.unice.fr
Tel: (+33) 4 93 37 76 30 Fax: (+33) 4 93 53 15 15
28 Avenue de Valombrose - 06107 NICE Cedex 2 - FRANCE
More information about the Python-list
mailing list