我们从Python开源项目中,提取了以下50个代码示例,用于说明如何使用xml.sax.make_parser()。
def scan(self): """Parse the *.qrc* files""" if not has_xml: Logs.error('no xml support was found, the rcc dependencies will be incomplete!') return ([], []) parser = make_parser() curHandler = XMLHandler() parser.setContentHandler(curHandler) fi = open(self.inputs[0].abspath(), 'r') try: parser.parse(fi) finally: fi.close() nodes = [] names = [] root = self.inputs[0].parent for x in curHandler.files: nd = root.find_resource(x) if nd: nodes.append(nd) else: names.append(x) return (nodes, names)
def parse(self): """ Loads the StyleSheets from the associated file, if it exists. """ try: if os.path.isfile(self.__file): parser = make_parser() parser.setContentHandler(SheetParser(self)) with open(self.__file) as the_file: parser.parse(the_file) except (IOError, OSError, SAXParseException): pass #------------------------------------------------------------------------ # # StyleSheet # #------------------------------------------------------------------------
def parse(self): """ Loads the OptionList from the associated file, if it exists. """ try: if os.path.isfile(self.filename): parser = make_parser() parser.setContentHandler(OptionParser(self)) parser.parse(self.filename) except (IOError, OSError, SAXParseException): pass #------------------------------------------------------------------------- # # OptionParser # #-------------------------------------------------------------------------
def parse(self): """ Loads the :class:`OptionList` from the associated file, if it exists. """ try: if os.path.isfile(self.filename): parser = make_parser() parser.setContentHandler(OptionParser(self)) with open(self.filename, encoding="utf-8") as the_file: parser.parse(the_file) except (IOError, OSError, SAXParseException): pass #------------------------------------------------------------------------- # # OptionParser # #-------------------------------------------------------------------------
def parse(self): """ Loads the :class:`OptionList` from the associated file, if it exists. """ try: if os.path.isfile(self.filename): parser = make_parser() parser.setContentHandler(DocOptionParser(self)) with open(self.filename, encoding="utf-8") as the_file: parser.parse(the_file) except (IOError, OSError, SAXParseException): pass #------------------------------------------------------------------------ # # DocOptionParser class # #------------------------------------------------------------------------
def parse(self): """ Loads the BookList from the associated file, if it exists. """ try: parser = make_parser() parser.setContentHandler(BookParser(self, self.dbase)) with open(self.file) as the_file: parser.parse(the_file) except (IOError, OSError, ValueError, SAXParseException, KeyError, AttributeError): LOG.debug("Failed to parse book list", exc_info=True) #------------------------------------------------------------------------- # # BookParser # #-------------------------------------------------------------------------
def __init__(self): self.name = SOURCE_NAME parser = make_parser() handler = SaintHandler() _file, r = conf.getFeedData(SOURCE_NAME, SOURCE_FILE) parser.setContentHandler(handler) parser.parse(BytesIO(_file)) self.cves = defaultdict(list) self.bids = defaultdict(list) self.osvdbs = defaultdict(list) for exploit in handler.exploits: data = copy.copy(exploit) if data.get('cve'): data.pop('cve') if exploit.get('cve'): self.cves[ exploit['cve'] ].append(data) if exploit.get('bid'): self.bids[ exploit['bid'] ].append(data) if exploit.get('osvdb'): self.osvdbs[exploit['osvdb']].append(data)
def __init__(self): self.name = "redhat" handlers = [{'handler': RPMHandler(), 'source': 'rpm' }, {'handler': RHSAHandler(), 'source': 'rhsa'} ] parser = make_parser() self.cves = defaultdict(dict) for handler in handlers: _file, r = conf.getFeedData(handler['source'], SOURCES[handler['source']]) parser.setContentHandler(handler['handler']) if type(_file) is bytes: _file = BytesIO(_file) parser.parse(_file) for cve, data in handler['handler'].CVEs.items(): self.cves[cve].update(data)
def __init__(self): self.name = SOURCE_NAME parser = make_parser() handler = D2secHandler() _file, r = conf.getFeedData(SOURCE_NAME, SOURCE_FILE) parser.setContentHandler(handler) parser.parse(BytesIO(_file)) self.cves = defaultdict(list) self.exploits = defaultdict(dict) for exploit in handler.exploits: _exploit = copy.copy(exploit) # clean exploit to add to the list _exploit.pop('refs') for ref in exploit.get('refs', []): if ref['type'] == 'cve': self.cves[ref['key']].append(_exploit) else: if ref['key'] not in self.exploits[ref['type']]: self.exploits[ref['type']][ref['key']] = [] self.exploits[ref['type']][ref['key']].append(_exploit)
def test_make_parser2(self): # Creating parsers several times in a row should succeed. # Testing this because there have been failures of this kind # before. from xml.sax import make_parser p = make_parser() from xml.sax import make_parser p = make_parser() from xml.sax import make_parser p = make_parser() from xml.sax import make_parser p = make_parser() from xml.sax import make_parser p = make_parser() from xml.sax import make_parser p = make_parser() # =========================================================================== # # saxutils tests # # ===========================================================================
def test_5027_1(self): # The xml prefix (as in xml:lang below) is reserved and bound by # definition to http://www.w3.org/XML/1998/namespace. XMLGenerator had # a bug whereby a KeyError is thrown because this namespace is missing # from a dictionary. # # This test demonstrates the bug by parsing a document. test_xml = StringIO( '<?xml version="1.0"?>' '<a:g1 xmlns:a="http://example.com/ns">' '<a:g2 xml:lang="en">Hello</a:g2>' '</a:g1>') parser = make_parser() parser.setFeature(feature_namespaces, True) result = StringIO() gen = XMLGenerator(result) parser.setContentHandler(gen) parser.parse(test_xml) self.assertEqual(result.getvalue(), start + ( '<a:g1 xmlns:a="http://example.com/ns">' '<a:g2 xml:lang="en">Hello</a:g2>' '</a:g1>'))
def test_5027_1(self): # The xml prefix (as in xml:lang below) is reserved and bound by # definition to http://www.w3.org/XML/1998/namespace. XMLGenerator had # a bug whereby a KeyError is raised because this namespace is missing # from a dictionary. # # This test demonstrates the bug by parsing a document. test_xml = StringIO( '<?xml version="1.0"?>' '<a:g1 xmlns:a="http://example.com/ns">' '<a:g2 xml:lang="en">Hello</a:g2>' '</a:g1>') parser = make_parser() parser.setFeature(feature_namespaces, True) result = self.ioclass() gen = XMLGenerator(result) parser.setContentHandler(gen) parser.parse(test_xml) self.assertEqual(result.getvalue(), start + ( '<a:g1 xmlns:a="http://example.com/ns">' '<a:g2 xml:lang="en">Hello</a:g2>' '</a:g1>'))
def main(): xmlString = "<note>\n<to>Tove</to>\n<from>Jani</from>\n<heading>Reminder</heading>\n<body>Don't forget me this weekend!</body>\n</note>" # bad xml.sax.parseString(xmlString, ExampleContentHandler()) xml.sax.parse('notaxmlfilethatexists.xml', ExampleContentHandler()) sax.parseString(xmlString, ExampleContentHandler()) sax.parse('notaxmlfilethatexists.xml', ExampleContentHandler) # good defusedxml.sax.parseString(xmlString, ExampleContentHandler()) # bad xml.sax.make_parser() sax.make_parser() print('nothing') # good defusedxml.sax.make_parser()
def test_5027_1(self): # The xml prefix (as in xml:lang below) is reserved and bound by # definition to http://www.w3.org/XML/1998/namespace. XMLGenerator had # a bug whereby a KeyError is raised because this namespace is missing # from a dictionary. # # This test demonstrates the bug by parsing a document. test_xml = StringIO( '<?xml version="1.0"?>' '<a:g1 xmlns:a="http://example.com/ns">' '<a:g2 xml:lang="en">Hello</a:g2>' '</a:g1>') parser = make_parser() parser.setFeature(feature_namespaces, True) result = self.ioclass() gen = XMLGenerator(result) parser.setContentHandler(gen) parser.parse(test_xml) self.assertEqual(result.getvalue(), self.xml( '<a:g1 xmlns:a="http://example.com/ns">' '<a:g2 xml:lang="en">Hello</a:g2>' '</a:g1>'))
def assertXMLWellFormed(self, stream, msg=None, context=2): """asserts the XML stream is well-formed (no DTD conformance check) :param context: number of context lines in standard message (show all data if negative). Only available with element tree """ try: from xml.etree.ElementTree import parse self._assertETXMLWellFormed(stream, parse, msg) except ImportError: from xml.sax import make_parser, SAXParseException parser = make_parser() try: parser.parse(stream) except SAXParseException as ex: if msg is None: stream.seek(0) for _ in range(ex.getLineNumber()): line = stream.readline() pointer = ('' * (ex.getLineNumber() - 1)) + '^' msg = 'XML stream not well formed: %s\n%s%s' % (ex, line, pointer) self.fail(msg)
def _flatsaxParse(fl): """ Perform a SAX parse of an XML document with the _ToStan class. @param fl: The XML document to be parsed. @type fl: A file object or filename. @return: a C{list} of Stan objects. """ parser = make_parser() parser.setFeature(handler.feature_validation, 0) parser.setFeature(handler.feature_namespaces, 1) parser.setFeature(handler.feature_external_ges, 0) parser.setFeature(handler.feature_external_pes, 0) s = _ToStan(getattr(fl, "name", None)) parser.setContentHandler(s) parser.setEntityResolver(s) parser.setProperty(handler.property_lexical_handler, s) parser.parse(fl) return s.document
def parse_to_iterate_probabilitytable(self): handler = SAX_Handler("iter", self.em_io_handler) parser = make_parser() parser.setContentHandler(handler) for count in range(self.NUM_TRAINING_ITERATION): print "Starting iteration: "+str(count+1)+" ..." print "Resetting all counts to ZERO ..." self.em_io_handler.reset_count_table() print "Start parsing "+self.training_xmlfile+" ..." parser.parse(self.training_xmlfile) print "Ending iteration: "+str(count+1)+" ..." print "Updating probability table ..." self.em_io_handler.update_probability_table()
def create_parser(target, store): parser = make_parser() try: # Workaround for bug in expatreader.py. Needed when # expatreader is trying to guess a prefix. parser.start_namespace_decl( "xml", "http://www.w3.org/XML/1998/namespace") except AttributeError: pass # Not present in Jython (at least) parser.setFeature(handler.feature_namespaces, 1) rdfxml = RDFXMLHandler(store) rdfxml.setDocumentLocator(target) # rdfxml.setDocumentLocator(_Locator(self.url, self.parser)) parser.setContentHandler(rdfxml) parser.setErrorHandler(ErrorHandler()) return parser
def parse(filename): """ Interprets the given string as a filename, URL or XML data string, parses it and returns a Python object which represents the given document. Raises ``ValueError`` if the argument is None / empty string. Raises ``xml.sax.SAXParseException`` if something goes wrong during parsing.s """ if filename is None or filename.strip() == '': raise ValueError('parse() takes a filename, URL or XML string') parser = make_parser() sax_handler = Handler() parser.setContentHandler(sax_handler) if os.path.exists(filename) or is_url(filename): parser.parse(filename) else: parser.parse(StringIO(filename)) return sax_handler.root
def __init__(self, pool, module, module_state): "Register known taghandlers, and managed tags." sax.handler.ContentHandler.__init__(self) self.pool = pool self.module = module self.ModelData = pool.get('ir.model.data') self.fs2db = Fs2bdAccessor(self.ModelData, pool) self.to_delete = self.populate_to_delete() self.noupdate = None self.module_state = module_state self.grouped = None self.grouped_creations = defaultdict(dict) self.grouped_write = defaultdict(list) self.grouped_model_data = [] self.skip_data = False Module = pool.get('ir.module') self.installed_modules = [m.name for m in Module.search([ ('state', 'in', ['installed', 'to upgrade']), ])] # Tag handlders are used to delegate the processing self.taghandlerlist = { 'record': RecordTagHandler(self), 'menuitem': MenuitemTagHandler(self), } self.taghandler = None # Managed tags are handled by the current class self.managedtags = ["data", "tryton"] # Connect to the sax api: self.sax_parser = sax.make_parser() # Tell the parser we are not interested in XML namespaces self.sax_parser.setFeature(sax.handler.feature_namespaces, 0) self.sax_parser.setContentHandler(self)
def load(self, source, content_types): """Return Json dic. 'source' is a source XML file source, 'content_type' is the content type to load""" parser = sax.make_parser() parser.setContentHandler(DictBuilder(content_types)) parser.setFeature(sax.handler.feature_namespaces, 1) inpsrc = urllib.request.urlopen(source) parser.parse(inpsrc) return parser.getContentHandler().jsonable()
def __init__(self, fh, **kwargs): """ Constructor. fh - a valid, open file handle to XML based record data. """ super(XMLRecordParser, self).__init__() self.xmlparser = make_parser() self.xmlparser.setContentHandler(SaxRecordParser(self.consume_record)) self.fh = fh self.__dict__.update(kwargs)
def __init__(self): self.name = SOURCE_NAME parser = make_parser() handler = VendorStatementsHandler() _file, r = conf.getFeedData(SOURCE_NAME, SOURCE_FILE) parser.setContentHandler(handler) parser.parse(_file) self.cves = handler.statements
def __init__(self): self.name = SOURCE_NAME parser = make_parser() handler = OVALHandler() _file, r = conf.getFeedData(SOURCE_NAME, SOURCE_FILE) parser.setContentHandler(handler) parser.parse(_file) self.cves = {} self.oval = handler.ovals
def test_make_parser(self): # Creating a parser should succeed - it should fall back # to the expatreader p = make_parser(['xml.parsers.no_such_parser']) # ===== XMLGenerator
def test_make_parser(self): # Creating a parser should succeed - it should fall back # to the expatreader p = make_parser(['xml.parsers.no_such_parser'])
def test_sf_1513611(self): # Bug report: http://www.python.org/sf/1513611 sio = StringIO("invalid") parser = make_parser() from xml.sax import SAXParseException self.assertRaises(SAXParseException, parser.parse, sio)