elementtree and entities

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Daniel Nogradi

    #1

    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
  • Duncan Booth

    #2
    Re: elementtree and entities

    "Daniel Nogradi" <nogradi@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.tostrin g( 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.

    Comment

    • Daniel Nogradi

      #3
      Re: elementtree and entities

      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.

      Comment

      Working...