xsl-list
[Top] [All Lists]

Re: XML generation from a text file

2003-11-22 09:37:06
Hi all,

I have a plain text file as following.
I want to read the text file line by line and add each line
as a value of element of an xml document.

The generated xml document will be as following.

Any suggestion?

Do you like Python ?
I could make it smaller but I had used the minidom package to
keep a relationship with xml :)

-----------------------8<-----------------------------------
import os, sys
import xml.dom.minidom

f=file(sys.argv[1])
result = file(sys.argv[2],"w")

doc = xml.dom.minidom.Document()
root=doc.createElementNS("", "someURIs")
doc.appendChild(doc.createProcessingInstruction("xml-stylesheet",
        "type=\"text/xsl\" href=\"multiple_input3.xsl\""))
doc.appendChild(root)

# read lines from in file
for line in f.readlines():
        node = doc.createElementNS("", "file")
        node.appendChild(doc.createTextNode(line))
        root.appendChild(node)
        
# write result
result.write(doc.toprettyxml(indent="  ",encoding="ISO-8859-1"))
-----------------------8<-----------------------------------

python foo.py Folder_structure.xml Folder_structure.txt

does it...

but it's a xml centric solution, no place here for xsl...

cheers



--
XPath free testing software :  http://lantern.sourceforge.net
Frédéric Laurent                     http://www.opikanoba.org






XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list



<Prev in Thread] Current Thread [Next in Thread>