2

I'm using minidom (among others) in Python to pull a list of files from a directory, get their modified times, other misc. data and then write that data to an XML file. The data prints just fine, but when I try to write the data to a file, I only get the XML for one of the files in the directory. Here is my code (I've removed a good amount of createElement and appendChild methods as well as any non-relevant variables for the sake of readability/space):

for filename in os.listdir((os.path.join('\\\\10.10.10.80\Jobs\success'))):

    doc = Document()
    modTime = datetime.datetime.fromtimestamp(os.path.getmtime('\\\\10.10.10.80\Jobs\success\\'+filename)).strftime('%I:%M:%S %p')
    done = doc.createElement('Printed Orders')
    doc.appendChild(done)
    ordernum = doc.createElement(filename)
    done.appendChild(ordernum)
    #This is where other child elements have been removed

    print doc.toprettyxml(indent='  ')
    xmlData = open(day_path, 'w')
    xmlData.write(doc.toprettyxml(indent='  '))

Hopefully this is enough to see what's going on. Since print returns the values I am expecting, I think that the write function is where I'm going wrong.

1 Answer 1

2

If I understood your itent

you mustn't create one different document for each file so you have to put the creation of the document and the writing of the xml file outside the loop

from xml.dom.minidom import Document 
import os,datetime
path = "/tmp/"
day_path ="today.xml"
doc = Document()
done = doc.createElement('Printed Orders')

for filename in os.listdir((os.path.join(path))):

    print "here"
    modTime = datetime.datetime.fromtimestamp(os.path.getmtime(path+filename)).strftime('%I:%M:%S %p')
    doc.appendChild(done)
    ordernum = doc.createElement(filename)
    done.appendChild(ordernum)
    #This is where other child elements have been removed

print doc.toprettyxml(indent='  ')
xmlData = open(day_path, 'w')
xmlData.write(doc.toprettyxml(indent='  '))

EDIT: for the HierarchyRequestErr error you have to put the creation of the root element outside the loop also

Sign up to request clarification or add additional context in comments.

3 Comments

That is my intent, one single file containing all data (Ultimately it will be one file created each day) I tried what you suggested, but the output file still only contains XML for one file in the os.listdir On top of that, print only returns one file as well, where as before it returned all of them. Also, moving doc = Document() outside of the loop returns the error: HierarchyRequestErr: two document elements disallowed
Next time create a self contenting example so we can test easely
Thanks @Xavier Combelle after moving the creation of the root element everything worked perfectly. Next time I'll make sure to provide a more test friendly example.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.