| Alan
|
9
|
 |
|
12-18-2004 04:09 PM ET (US)
|
|
Preserving comments in ElementTree
Is there a way to preserve comments using ElementTree?
I thought it might have been something I was doing, but even the simplest possible round trip:
ElementTree.ElementTree(file='file-with-comments.xml').write('file2.xml')
... loses comments that were in the original file. Which is catastrophic (ok, means going and getting the backup copy) if you are actually writing back to the same file and had temporarily (you thought) commented something out.
|
Fredrik Lundh
|
10
|
 |
|
12-19-2004 04:26 PM ET (US)
|
|
The ElementTree class is not really designed for round-tripping of human-authored documents; the parsers are only concerned about the infoset, and the tree writer will happily use its own way to encode things, completely ignoring whether something was originally a character reference or an entity or a CDATA section, etc.
You can add comments to trees, though, so it should be possible to tweak one of the parsers so it preserves comments. I'll see if I can dig up an example...
|