DOM text to xml aarrgghhh!!!!

huntermorgan tompol at hotmail.com
Wed Jun 11 21:24:57 EDT 2003


hi there, ive posted b4 but was mentioned that my request was a bit
vague so.....im a tertiary student getting my head around python and
im liking python but not quite handy with the syntax etc...so im
really needing help on this one

i would like to have someone look at my code below and answer as to
what i have done that makes my small program not work and some code to
fix it.

it is a small python coded program that takes in a tertiary course
outline ("Outline2003") in a text format and should output it in an
xml styled text format. hence, text in = xml out.

at the moment the output file reads this

<?xml version="1.0" ?>
<2003 Course Outline/>

...and thats it. There are headings and sub-headings which should
format to xml tags and the text in between should just be written to
the output as is, in between the tags.

so please...im desperate to figure out why my code wont work.

can anybody help???? below is the code and below that is a small
snipet of the course outline

<start of code>

import re
from xml.dom.minidom import *

def main(arg):
    try:
        f = open(arg)
    except:
        print "cannot open file"


    newdocument = Document()
    rootElement = newdocument.createElement("2003 Course Outline")
    newdocument.appendChild(rootElement)
    
    tagSequence = re.compile("(^\d+)\t+")
    
    while 1:
        line = f.readline()
        if not line:
            break

        s = line

        target = tagSequence.search(s)
        if target:
            s2 = re.search("\t", s)
            result = s[s2.span()[1]:]
            newElement = newdocument.createElement(result)
            newDocument.appendChild(newElement)
    
    x = newdocument.toxml()
    f=open('CourseOutlineXML.xml', 'w')
    f.write(x)
    print x
                      
if __name__ == '__main__':
    main("Outline2003.txt")

<end of code>

<start of "Outline2003">

1	COURSE STAFF MEMBERS

	(a)	Course Academic Staff Member (#sub heading)
                Rob Oliver - Room number S662. Contact number 900 0000

	(b)	Programme Leader (#sub heading)
                Trevor Nesbit, Room number N215. Contact number 900
0000

        (c)	Course Co-ordinator (#sub heading)
                Dr Mike Lance - Room number S661 Contact number 900
0000

        (d) 	Head of School (Acting) (#sub heading)
		Janne Ross, Room number S176, Contact number 900 0000


2	MATERIALS
	NIL

3	CLASS HOURS AND TIMES

       Day	Time	Room
       Tuesday	10:00 - 12:00	X307
       Thursday	10:00 - 12:00	L249


4	REFERENCE TO STUDENT HANDBOOKS

	Students should obtain a copy of the following

	Christchurch Polytechnic Student Handbook
	Faculty of Commerce Student Handbook
	Programme Handbook

<end of "Outline2003">




More information about the Python-list mailing list