468,301 Members | 1,449 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,301 developers. It's quick & easy.

elementtree and entities

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
2 2548
"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
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.

Similar topics

7 posts views Thread by Stewart Midwinter | last post: by
1 post views Thread by Greg Wilson | last post: by
4 posts views Thread by alainpoint | last post: by
9 posts views Thread by Chris Spencer | last post: by
1 post views Thread by mirandacascade | last post: by
15 posts views Thread by Steven Bethard | last post: by
reply views Thread by Greg Aumann | last post: by
2 posts views Thread by mirandacascade | last post: by
2 posts views Thread by =?ISO-8859-1?Q?J=2E_Pablo_Fern=E1ndez?= | last post: by
reply views Thread by NPC403 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.