turnerca902 0 Newbie Poster

Hi Folks,

For thoes of you who may have read and offered assistance on this problem already, I thank you. (Woooee, Slate.)

The project/problem has been evolving and now I feel I am getting close to a solution...but still a few steps away.

I have the code below, which is designed to read thru the files in a given folder, determine which of thoes files are XML's and then find the desired XML tag - "Title" and change the text in that tag.

from xml.dom import minidom
# list just the files in a given folder
folder = 'E:/Manifold/4555630/XMLPARSE/'
for (paths, dirs, files) in os.walk(folder):
    # testing, this shows a list of filenames
    print files
    # now loop through the list of filenames
    for filename in files:
        # testing
        print filename
        #now enter code to process each of your files
        filesplit = filename.split('.')
        filenameroot = filesplit[0]
        try:
            fileext = filesplit[2]
        except:
            print 'no xml here'
        else:
            if fileext == 'xml':

                xmldoc = minidom.parse(filename)
                xmldoc
                print xmldoc.toxml()
                print xmldoc.toprettyxml()
                reflist = xmldoc.getElementsByTagName('title')
                bitref = reflist[0]
                print bitref.toxml()
                print bitref.firstChild.data
                bitref.firstChild.replaceData(0,len(bitref.firstChild.data),'BOO BOO Bear')
                print bitref.firstChild.data
                print xmldoc.toprettyxml()
                fp = open(filename, 'w')
                xmldoc.writexml(fp, "     ", "", "\n")
                fp.close()
                xmldoc.unlink()

Next, I have a little preliminary part that reads my "reference" file and spits out the contents.

import csv
import sys

f = open('TEST_LUT.csv', "rt")
try:
    reader = csv.reader(f)
    for row in reader:
        print row

finally:
    f.close()

produces:

>>> []
>>>
>>>
>>>

My filename for my XML files always matches an entry in the Reference file. I would like to figure out how to take the "TITLE" value and substitute it into the XML where I have currently placed a 'dummy value' of "BOO BOO Bear." (The original value in the Title tag was the file name. (i.e. 4555630.jpg.)

So, I'm just putting this out there...I would be grateful for your help.

Thanks

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.