2

I have an xml.dom.Element object and would like to convert it to a string containing XML. There doesn't seem to be a method for this or am I missing something?

1 Answer 1

4

Use the toprettyxml or toxml method:

elt.toprettyxml(indent = '  ')

For example,

import xml.dom.minidom as minidom
doc = minidom.Document()
foo = doc.createElement("foo")
doc.appendChild(foo)
print(foo.__class__)
# xml.dom.minidom.Element

print(foo.toprettyxml(indent = '  '))
# <foo/>
Sign up to request clarification or add additional context in comments.

Comments

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.