tag replacement in toxml()

Gabriel Genellina gagsl-py2 at yahoo.com.ar
Sun Apr 1 05:34:12 EDT 2007


En Sun, 01 Apr 2007 05:26:48 -0300, Manuel Ospina <manuelospina at yahoo.es>  
escribió:

> I am new on the list and I already have a question :-(.

Welcome!

> I have something like this:
>
> 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>'

That's right...

> My question is how I can avoid that toxml() replaces the tags?

createTextNode is used to create a *text* node: its argument is  
interpreted as the node contents, and quoted as appropiate. What if you  
want it to say "Price<1000"? The < sign must be quoted.

You need a text node AND a <b> node, both children of nodeTwo.

Note: Using ElementTree is a lot easier!

-- 
Gabriel Genellina




More information about the Python-list mailing list