I am using Python xml.etree.ElementTree to output XML. I want to output it with entity references that will be substituted when the XML is parsed.
ordinarily '&' is escaped as &
because '&' is used to declare entity references. However, I really do want to write an entity reference. For example, I want to write an XML file containing the entity reference &manifestName;
:
>>> from xml.etree.ElementTree import Element, tostring
>>> manifest = Element('manifest')
>>> manifest.text = '&manifestName;'
>>> tostring(manifest)
Which returns an escaped ampersand:
'<manifest>&manifestName;</manifest>'
The desired XML would be:
'<manifest>&manifestName;</manifest>'
I have tried various escaping tricks, like &
, &
, &&
, but they do not work. The ampersands they contain are always rendered as &
.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…