tag replacement in toxml()
"Martin v. Löwis"
martin at v.loewis.de
Sun Apr 1 10:56:19 EDT 2007
> import xml.dom.minidom
> from xml.dom.minidom import getDOMImplementation
> impl = getDOMImplementation()
> myDoc = impl.createDocument(None, "example", None)
> myRoot = myDoc.documentElement
> myNode1 = myDoc.createElement("node")
> myNode2 = myDoc.createElement("nodeTwo")
> myText = myDoc.createTextNode("Here is the <b>problem</>")
> myNode2.appendChild(myText)
> myNode1.appendChild(myNode2)
> myRoot.appendChild(myNode1)
> print myDoc.toxml()
>
> The result is:
> '<?xml version="1.0" ?>\n<example><node><nodeTwo>Here is the <b>problem</></nodeTwo></node></example>'
>
>
> My question is how I can avoid that toxml() replaces the tags?
Gabriel already answered the question: you need to add a 'b'
element, which has a text child with the text 'problem'; this
b element needs to be a sibling of the text node 'Here is the '.
This still won't give you the output "Here is the <b>problem</>",
as that will insert a closing tag. If you really want to produce
the text
'<?xml version="1.0" ?>\n<example><node><nodeTwo>Here is the
<b>problem</></nodeTwo></node></example>'
you cannot use an XML library to do so: this text is not
well-formed XML (because </> is illegal syntax).
Regards,
Martin
More information about the Python-list
mailing list