You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
minidom: AttributeError: DocumentFragment instance has no attribute 'writexml' #54092
Summary:
Writing a document fragment doesn't work because the 'writexml' method is not implemented.
Problem:
I would like to be able to write out document fragments as XML text, but this functionality is not implemented. Here are the reasons why I think this functionality should be implemented.
DOM Level 1 describes a document fragment as a lightweight document. I can write a document as XML text so why not a document fragment?
This would be very easy to implement, basically just the child processing loop from Element.writexml:
for node in self.childNodes:
node.writexml(writer,indent+addindent,addindent,newl)
Document fragments are often returned as intermediate results in XML building. The best way I can think of to unit test these results is by writing the document fragment as XML text and comparing to an existing string. (Comparing two document fragments is not guaranteed to work.)
Implementing an XML editor on top of minidom would require this functionality, e.g. display a piece of cut XML.
I realize that writing document fragments as XML text is not a common or "core" operation, but it makes sense in the above four cases and perhaps other cases as well. Therefore, to me, implementing this functionality is less a question of "Why?" and more a question of "Why not?"
I will be glad to add any other information as requested.
Seems like a reasonable request to me, even if it hasn't been touched or re-requested for years.
Funny enough, DocumentFragment is currently documented as "not implemented" (as for Entity, Notation, CDATASection, CharacterData, DOMImplementation, DocumentType). I'll add a PR to clean those up.
New changeset 2ea8099 by Serhiy Storchaka (Stefan Behnel) in branch 'master': bpo-9883: Update list of unimplemented interfaces in minidom. (GH-12677) 2ea8099
Activity
AubreyBarnard commentedon Sep 17, 2010
Summary:
Writing a document fragment doesn't work because the 'writexml' method is not implemented.
Problem:
I would like to be able to write out document fragments as XML text, but this functionality is not implemented. Here are the reasons why I think this functionality should be implemented.
DOM Level 1 describes a document fragment as a lightweight document. I can write a document as XML text so why not a document fragment?
This would be very easy to implement, basically just the child processing loop from Element.writexml:
for node in self.childNodes:
node.writexml(writer,indent+addindent,addindent,newl)
Document fragments are often returned as intermediate results in XML building. The best way I can think of to unit test these results is by writing the document fragment as XML text and comparing to an existing string. (Comparing two document fragments is not guaranteed to work.)
Implementing an XML editor on top of minidom would require this functionality, e.g. display a piece of cut XML.
I realize that writing document fragments as XML text is not a common or "core" operation, but it makes sense in the above four cases and perhaps other cases as well. Therefore, to me, implementing this functionality is less a question of "Why?" and more a question of "Why not?"
I will be glad to add any other information as requested.
BreamoreBoy commentedon Oct 2, 2014
Can someone please confirm that my assessment of this as an enhancement is correct, thanks.
freddrake commentedon May 23, 2017
I agree that writexml should be available for document fragments.
I doubt the additional level of indentation should be added, as you've included in point 2.
freddrake commentedon Apr 3, 2019
Updated target to Python 3.8, since this has aged a bit.
scoder commentedon Apr 3, 2019
Seems like a reasonable request to me, even if it hasn't been touched or re-requested for years.
Funny enough, DocumentFragment is currently documented as "not implemented" (as for Entity, Notation, CDATASection, CharacterData, DOMImplementation, DocumentType). I'll add a PR to clean those up.
https://docs.python.org/3/library/xml.dom.minidom.html#minidom-and-the-dom-standard
serhiy-storchaka commentedon Apr 6, 2019
New changeset 2ea8099 by Serhiy Storchaka (Stefan Behnel) in branch 'master':
bpo-9883: Update list of unimplemented interfaces in minidom. (GH-12677)
2ea8099
miss-islington commentedon Apr 8, 2019
New changeset a9a065a by Miss Islington (bot) in branch '3.7':
bpo-9883: Update list of unimplemented interfaces in minidom. (GH-12677)
a9a065a
csabella commentedon May 30, 2019
I've closed PR1745 as it appeared to be abandoned.
Merge branch 'main' into pythongh-54092-fragment-writexml
serhiy-storchaka commentedon Sep 13, 2026
Fixed in 3.16 by #156646:
DocumentFragment.writexml()is implemented, andtoxml()/toprettyxml()work on document fragments.Merge remote-tracking branch 'upstream/main' into pythongh-54092-frag…
Merge remote-tracking branch 'origin/pythongh-54092-fragment-writexml…
gh-54092: Implement writexml() for DocumentFragment in xml.dom.minidom (
pythongh-54092: Implement writexml() for DocumentFragment in xml.dom.…