escaping illegal characters in XML

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

An article by Uche Ogbuji
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')
sources = doc.createElement('item')
sources.setAttribute('harvested', 'blah blah')
print doc.toprettyxml()

only produces:

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

instead of:

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

Anybody have any ideas?


More information about the Python-list mailing list