Search code examples
pythonxmlminidom

Can not save xml file using minidom


I tried to modify and save a xml file using minidom in python.

Everything is quite working good except 1 specific file, that I only can read but can not write it back.

Code that I use to save xml file:

domXMLFile = minidom.parse(dom_document_filename)

#some modification

F= open(dom_document_filename,"w")
domXMLFile .writexml(F)
F.close()

My question is :
Is it true that minidom can not handle too large file ( 714KB )?

How do i solve my problem?


Solution

  • In my opinion, lxml is way better than minidom for handling XML. If you have it, here is how to use it:

    from lxml import etree 
    root = etree.parse('path/file.xml')
    
    # some changes to root
    
    with open('path/file.xml', 'w') as f:
         f.write(etree.tostring(root, pretty_print=True))
    

    If not, you could use pdb to debug your code. Just write import pdb; pdb.set_trace() in your code where you want a break pont and when running your function in a shell, it should stop at this line. It may give you a better view for what is not working.