I want to be able to edit existing XML config files via Python while preserving the formatting of the file and the comments in them so that its still human readable.
I will be updating existing XML elements and changing values as well as adding new XML elements to the file.
Available XML parsers such as ElementTree and lxml are great ways to edit XML files but you loose the original formatting(when adding new elements to the file) and comments that were in the file.
Using Regular expressions seems to be an option but I know that this is not recommended with XML.
So I'm looking for something along the lines of a Pythonic XML file editor. What is the best way to go about this? Thanks.
I recommend you to parse the XML document using a SAX parser, this gives you great flexibility to make your changes and to write back the document as it was.
Take a look at the xml.sax modules (see Python's documentation).
What is the best option for editing XML files in Python that preserve the original formatting of the file?