def _detectEncoding(self, xml_data, isHTML=False):
xml_encoding = sniffed_xml_encoding = None<EOL>try:<EOL><INDENT>if xml_data[:<NUM_LIT:4>] == '<STR_LIT>':<EOL><INDENT>xml_data = self._ebcdic_to_ascii(xml_data)<EOL><DEDENT>elif xml_data[:<NUM_LIT:4>] == '<STR_LIT>':<EOL><INDENT>sniffed_xml_encoding = '<STR_LIT>'<EOL>xml_data = str(xml_data, '<STR_LIT>').encode('<STR_LIT:utf-8>')<EOL><DEDENT>elif (len(xml_data) >= <NUM_LIT:4>) and (xml_data[:<NUM_LIT:2>] == '<STR_LIT>')and (xml_data[<NUM_LIT:2>:<NUM_LIT:4>] != '<STR_LIT>'):<EOL><INDENT>sniffed_xml_encoding = '<STR_LIT>'<EOL>xml_data = str(xml_data[<NUM_LIT:2>:], '<STR_LIT>').encode('<STR_LIT:utf-8>')<EOL><DEDENT>elif xml_data[:<NUM_LIT:4>] == '<STR_LIT>':<EOL><INDENT>sniffed_xml_encoding = '<STR_LIT>'<EOL>xml_data = str(xml_data, '<STR_LIT>').encode('<STR_LIT:utf-8>')<EOL><DEDENT>elif (len(xml_data) >= <NUM_LIT:4>) and (xml_data[:<NUM_LIT:2>] == '<STR_LIT>') and(xml_data[<NUM_LIT:2>:<NUM_LIT:4>] != '<STR_LIT>'):<EOL><INDENT>sniffed_xml_encoding = '<STR_LIT>'<EOL>xml_data = str(xml_data[<NUM_LIT:2>:], '<STR_LIT>').encode('<STR_LIT:utf-8>')<EOL><DEDENT>elif xml_data[:<NUM_LIT:4>] == '<STR_LIT>':<EOL><INDENT>sniffed_xml_encoding = '<STR_LIT>'<EOL>xml_data = str(xml_data, '<STR_LIT>').encode('<STR_LIT:utf-8>')<EOL><DEDENT>elif xml_data[:<NUM_LIT:4>] == '<STR_LIT>':<EOL><INDENT>sniffed_xml_encoding = '<STR_LIT>'<EOL>xml_data = str(xml_data, '<STR_LIT>').encode('<STR_LIT:utf-8>')<EOL><DEDENT>elif xml_data[:<NUM_LIT:4>] == '<STR_LIT>':<EOL><INDENT>sniffed_xml_encoding = '<STR_LIT>'<EOL>xml_data = str(xml_data[<NUM_LIT:4>:], '<STR_LIT>').encode('<STR_LIT:utf-8>')<EOL><DEDENT>elif xml_data[:<NUM_LIT:4>] == '<STR_LIT>':<EOL><INDENT>sniffed_xml_encoding = '<STR_LIT>'<EOL>xml_data = str(xml_data[<NUM_LIT:4>:], '<STR_LIT>').encode('<STR_LIT:utf-8>')<EOL><DEDENT>elif xml_data[:<NUM_LIT:3>] == '<STR_LIT>':<EOL><INDENT>sniffed_xml_encoding = '<STR_LIT:utf-8>'<EOL>xml_data = str(xml_data[<NUM_LIT:3>:], '<STR_LIT:utf-8>').encode('<STR_LIT:utf-8>')<EOL><DEDENT>else:<EOL><INDENT>sniffed_xml_encoding = '<STR_LIT:ascii>'<EOL>pass<EOL><DEDENT><DEDENT>except:<EOL><INDENT>xml_encoding_match = None<EOL><DEDENT>xml_encoding_match = re.compile(<EOL>'<STR_LIT>').match(xml_data)<EOL>if not xml_encoding_match and isHTML:<EOL><INDENT>regexp = re.compile('<STR_LIT>', re.I)<EOL>xml_encoding_match = regexp.search(xml_data)<EOL><DEDENT>if xml_encoding_match is not None:<EOL><INDENT>xml_encoding = xml_encoding_match.groups()[<NUM_LIT:0>].lower()<EOL>if isHTML:<EOL><INDENT>self.declaredHTMLEncoding = xml_encoding<EOL><DEDENT>if sniffed_xml_encoding and(xml_encoding in ('<STR_LIT>', '<STR_LIT>', '<STR_LIT>',<EOL>'<STR_LIT>', '<STR_LIT>', '<STR_LIT>',<EOL>'<STR_LIT>', '<STR_LIT>', '<STR_LIT>', '<STR_LIT>',<EOL>'<STR_LIT>', '<STR_LIT>')):<EOL><INDENT>xml_encoding = sniffed_xml_encoding<EOL><DEDENT><DEDENT>return xml_data, xml_encoding, sniffed_xml_encoding<EOL>
Given a document, tries to detect its XML encoding.