我们从Python开源项目中,提取了以下3个代码示例,用于说明如何使用xml.etree.cElementTree.XMLParser()。
def parseXML(self): assert self.filepath.endswith(XML_EXT), "Unsupport file format" parser = etree.XMLParser(encoding='utf-8') xmltree = ElementTree.parse(self.filepath, parser=parser).getroot() filename = xmltree.find('filename').text try: verified = xmltree.attrib['verified'] if verified == 'yes': self.verified = True except KeyError: self.verified = False for object_iter in xmltree.findall('object'): typeItem = object_iter.find('type') # print(typeItem.text) if typeItem.text == 'bndbox': bndbox = object_iter.find("bndbox") label = object_iter.find('name').text # Add chris difficult = False if object_iter.find('difficult') is not None: difficult = bool(int(object_iter.find('difficult').text)) self.addShape(label, bndbox, difficult) # You Hao 2017/06/21 # add to load robndbox elif typeItem.text == 'robndbox': robndbox = object_iter.find('robndbox') label = object_iter.find('name').text difficult = False if object_iter.find('difficult') is not None: difficult = bool(int(object_iter.find('difficult').text)) self.addRotatedShape(label, robndbox, difficult) else: pass return True
def get_params_form_data(self): result = [] type_mapping = \ { 'hudson.model.StringParameterDefinition': ('CharField', {}) , 'hudson.model.TextParameterDefinition': ('CharField', {'widget': forms.widgets.Textarea}) , 'hudson.model.BooleanParameterDefinition': ('BooleanField', {}) , 'hudson.model.ChoiceParameterDefinition': ('ChoiceField', {}) } config = self.get_config().encode('utf-8') #parser = etree.XMLParser(encoding="utf-8") tree = etree.fromstring(config) params_root = tree.findall('properties/hudson.model.ParametersDefinitionProperty/parameterDefinitions') if params_root: params = params_root[0].getchildren() for param in params: name = getattr(param.find('name'), 'text', None) if not name: continue field_type, kwargs = type_mapping.get(param.tag, None) if not type: continue result.append\ ( { 'name': name , 'type': field_type , 'description': getattr(param.find('description'), 'text', None) , 'initial': getattr(param.find('defaultValue'), 'text', None) , 'choices': [(node.text, node.text) for node in param.findall('choices/a/string')] , 'kwargs': kwargs } ) return result
def check_loading_issues(self): """Check for any loading issues.""" missing_libs = [] tips = [] label = """<html> <body style='margin: 10px'> <b>The following dependencies could not be loaded successfully:</b><br> <ul><li>%s</li></ul><br> <b>Tips:</b><br> <ul><li>%s</li><br></ul> <b>For detailed information on how to load the plugin, see:</b><br> <ul> <li> <a href='#'>https://github.com/VirtueSecurity/aws-extender#getting-started</a> </li> </ul> </body> </html>""" if not RUN_TESTS: missing_libs.append('boto/boto3') tips.append('Make sure that the boto/boto3 library is installed properly, and\ the right path is specified in the "Folder for loading modules" setting.') try: CET.fromstring('<test></test>') except SAXException: # Try to workaround "http://bugs.jython.org/issue1127" try: def xml_parser(**_): class Parser(object): def feed(*_): raise XMLParseError def close(*_): return None return Parser() CET.XMLParser = xml_parser except TypeError: missing_libs.append('SAXParser') tips.append("""Run Burp Suite using the following command: <br><code style='background: #f7f7f9; color: red'>$ java -classpath xercesImpl.jar;burpsuite_pro.jar burp.StartBurp</code>""") if not missing_libs: return label %= ('</li><li>'.join(missing_libs), '</li><li>'.join(tips)) self.show_errors(label)