By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
439,985 Members | 1,574 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 439,985 IT Pros & Developers. It's quick & easy.

elementtree and entities

P: n/a
Hi list,

How does one prevent elementtree converting & to & (and similarly
for other entities)?
>>from xml.etree import ElementTree as et
x = et.Element( 'test' )
x.text = '&'
et.tostring( x )
'<test>&amp;</test>'

Sometimes I would like to have the output '<test>&</test>'

Daniel
May 13 '07 #1
Share this Question
Share on Google+
2 Replies


P: n/a
"Daniel Nogradi" <no*****@gmail.comwrote:
Hi list,

How does one prevent elementtree converting & to &amp; (and similarly
for other entities)?
>>>from xml.etree import ElementTree as et
x = et.Element( 'test' )
x.text = '&'
et.tostring( x )
'<test>&amp;</test>'

Sometimes I would like to have the output '<test>&</test>'

Daniel
elementtree is for processing xml. If you want to output something which
isn't xml then you'll have to use a different library or mess about with
the xml after it has been generated:

et.tostring(x).replace('&amp;', '&')

does what you want, but you won't be able to parse it again with anything
which expects xml.
May 13 '07 #2

P: n/a
How does one prevent elementtree converting & to &amp; (and similarly
for other entities)?
>>from xml.etree import ElementTree as et
x = et.Element( 'test' )
x.text = '&'
et.tostring( x )
'<test>&amp;</test>'

Sometimes I would like to have the output '<test>&</test>'

elementtree is for processing xml. If you want to output something which
isn't xml then you'll have to use a different library or mess about with
the xml after it has been generated:

et.tostring(x).replace('&amp;', '&')

does what you want, but you won't be able to parse it again with anything
which expects xml.
Thanks for the reply, I'll just live with replace then.
May 13 '07 #3

This discussion thread is closed

Replies have been disabled for this discussion.