escaping illegal characters in XML

Sandy Norton sandskyfly at hotmail.com
Sat Jan 11 22:07:18 CET 2003


An article by Uche Ogbuji
http://www.xml.com/lpt/a/2002/11/13/py-xml.html
was very helpful in solving the described problem. Hope this helps
someone else.

Now if I can only figure out how to insert the encoding attribute
using minidom. Setting the 'encoding' attribute of the Document
element does not seem to do the trick:

from xml.dom.minidom import Document

doc = Document()
doc.encoding = 'UTF-8'
root = doc.createElement('section')
doc.appendChild(root)
sources = doc.createElement('item')
sources.setAttribute('harvested', 'blah blah')
root.appendChild(sources)
print doc.toprettyxml()

only produces:

<?xml version="1.0" ?>
<section>
	<item harvested="blah blah"/>
</section>

instead of:

<?xml version="1.0" encoding="UTF-8" ?> 
<section>
	<item harvested="blah blah"/>
</section>


Anybody have any ideas?


Sandy




More information about the Python-list mailing list