the problems encountered in the project are recorded as follows to be useful to others.
def valid_xml_char_ordinal (c ): "" @ summary: check if the char is a valid XML character @ Param C: the character to be checked @ see: # http://www.w3.org/TR/2008/REC-xml-20081126/#charsets @ result: True/false " " return (# conditions ordered by presumed frequency 0x20 <= C <= 0xd7ff or C in (0x09, 0x0a, 0x0d ) or 0xe000 <= C <= 0 xfffd or 0x10000 <= C <= 0x10ffff )
Other illegal xml characters are encrypted by base64.CodeAs follows:
Try : Import XML. Sax. saxutils Except Importerror: Raise Importerror ( " Requires XML. Sax. saxutils package, pleas check if XML. Sax. saxutils is installed! " ) Import Base64 Import Logginglogger = Logging. getlogger ( _ Name __ ) _ All __ = [ " Escape " , " Unescape " ] Def Escape (data ): """ @ Summary: Escape '&', '<', and '>' in a string of data. if the data is not ASCII, then encode in base64 @ Param data: the data to be processed @ return {"base64": True | false, "data": Data} """ # Check if all of the data is in ASCII code Is_base64 = False escaped_data = "" Try : # Data. Decode ("ASCII ") If DataIs None: Data = "" Is_base64 = False For C In Data: If Not Valid_xml_char_ordinal (c): is_base64 = True Break # Check if need base64 encode If Is_base64: logger. debug ( " % S is not ascii-encoded string, so I will encode it in base64 " ) # Base64 encode Escaped_data = Base64.b64encode (data) Else : # Check if the data shoshould be escaped to be stored in XML Escaped_data = XML. Sax. saxutils. Escape (data) Except Exception, E: logger. excpetion (E) Return { " Base64 " : Is_base64, " Data " : Escaped_data} Def Unescape (data, is_base64 = False ): """ @ Summary: scape '& amp;', '& lt;', and' & gt; 'in a string of data. if base64 is true, then base64 decode will be processed first @ Param data: the data to be processed @ Param base64: Specify if the data is encoded by base64 @ result: unescaped data """ # Check if base64 Unescaped_data = Data If Is_base64: Try : Unescaped_data = Base64.b64decode (data) Except Exception, EX: logger. debug ( " Some excpetion occured when invoke b64decode " ) Logger. Error (Ex) Print Ex Else : # Unescape it Unescaped_data =XML. Sax. saxutils. Unescape (data) Return Unescaped_data