Hello friends, I have a small problem with creating XML documents. After creating the XML when I try to get a XML document as string using .toxml() or .toprettyxml() functions i got escaped string.

>>> from xml.dom.minidom import Document
>>> html = '<a href="http://www.google.com/">Google</a>'
>>> doc = Document()
>>> text = doc.createTextNode(html)
>>> text.data
'<a href="http://www.google.com/">Google</a>'
>>> text.toxml()
'&lt;a href=&quot;http://www.google.com/&quot;&gt;Google&lt;/a&gt;'
# escaped string

So, my question is how to get the XML document as not escaped string using .toxml(encoding) ?

Your <a href="http://www.google.com/">Google</a> is not a text node in your html document, but a <a> node. You should probably write something like

from xml.dom.minidom import Document

doc = Document()
link = doc.createElement("a")
link.attributes["href"] = "http://www.google.com/"
google = doc.createTextNode("Google")

print doc.toxml()

""" my output -->
<?xml version="1.0" ?>
<a href="http://www.google.com/">Google</a>

Special characters in raw strings MUST be escaped in the XML specification.

Edited 5 Years Ago by Gribouillis: n/a

This question has already been answered. Start a new discussion instead.