[XML-SIG] processing "special characters" efficiently

THOMAS PASSIN tpassin@idsonline.com
Thu, 6 Apr 2000 21:47:54 -0400


You might be able to wait and do a replace on an entire string after you
have accumulated it, rather than character-by-character like your example.
Regular expresssions would work well for this.  But if you really want to go
char-by-char, try this and see if it's not faster:

import string
MAGIC_CHARS="<&>'"+'"'
ESCAPES=["&lt;","&amp;","&gt;","&apos;","&quot;"]

def test(char):
    if char in MAGIC_CHARS:
        return ESCAPES[string.find(MAGIC_CHARS,char)]

if __name__=="__main__":
    c="&"
    print test(c)


Tom Passin
============================================================================
=========================
<Craig.Curtin@wdr.com> asked -
i'm looking for an efficient mechanism for filtering out
XML special characters....

the following code executes in slo-mo, can anyone
identify a more efficient way? this is obviously not
optimal. any help is appreciated.

thanks,

craig

text2XMLSpecialCharacters={   '&': '&amp;',
                        '<': '&lt;',
                        '>': '&gt;',
                  }

for k in text2XMLspecialChars.keys():
      data = string.replace(data, k, text2XMLspecialChars[k])