tag replacement in toxml()

M

Manuel Ospina

Hi all,

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

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 &lt;b&gt;problem&lt;/&gt;</nodeTwo></node></example>'


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

Regards,
Manuel





______________________________________________
LLama Gratis a cualquier PC del Mundo.
Llamadas a fijos y móviles desde 1 céntimo por minuto.
http://es.voice.yahoo.com
 
?

=?ISO-8859-1?Q?=22Martin_v=2E_L=F6wis=22?=

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 &lt;b&gt;problem&lt;/&gt;</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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,756
Messages
2,569,533
Members
45,007
Latest member
OrderFitnessKetoCapsules

Latest Threads

Top