我们从Python开源项目中,提取了以下24个代码示例,用于说明如何使用xml.sax.SAXParseException()。
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 test_sax_parse_exception_str(self): # pass various values from a locator to the SAXParseException to # make sure that the __str__() doesn't fall apart when None is # passed instead of an integer line and column number # # use "normal" values for the locator: str(SAXParseException("message", None, self.DummyLocator(1, 1))) # use None for the line number: str(SAXParseException("message", None, self.DummyLocator(None, 1))) # use None for the column number: str(SAXParseException("message", None, self.DummyLocator(1, None))) # use None for both: str(SAXParseException("message", None, self.DummyLocator(None, None)))
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 test_expat_incomplete(self): parser = create_parser() parser.setContentHandler(ContentHandler()) # do nothing self.assertRaises(SAXParseException, parser.parse, StringIO("<foo>")) self.assertEqual(parser.getColumnNumber(), 5) self.assertEqual(parser.getLineNumber(), 1)
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)
def parseXmlFile(xmlFile, handler): """ Parses XML file by a given handler """ try: with contextlib.closing(StringIO(readCachedFileContent(xmlFile))) as stream: parse(stream, handler) except (SAXParseException, UnicodeError), ex: errMsg = "something appears to be wrong with " errMsg += "the file '%s' ('%s'). Please make " % (xmlFile, getSafeExString(ex)) errMsg += "sure that you haven't made any changes to it" raise SqlmapInstallationException, errMsg
def parseXmlFile(xmlFile, handler): """ Parses XML file by a given handler """ try: with contextlib.closing(StringIO(readCachedFileContent(xmlFile))) as stream: parse(stream, handler) except (SAXParseException, UnicodeError), ex: errMsg = "something seems to be wrong with " errMsg += "the file '%s' ('%s'). Please make " % (xmlFile, ex) errMsg += "sure that you haven't made any changes to it" raise SqlmapInstallationException, errMsg
def test_expat_incomplete(self): parser = create_parser() parser.setContentHandler(ContentHandler()) # do nothing self.assertRaises(SAXParseException, parser.parse, StringIO("<foo>"))