我们从Python开源项目中,提取了以下23个代码示例,用于说明如何使用xml.sax()。
def __init__(self, rules = None): xml.sax.handler.ContentHandler.__init__(self) self.body = None self.header = None self.attrs = {} self._data = None self._next = "E" # Keeping state for message validity self._stack = [self.Frame('SOAP')] # Make two dictionaries to store the prefix <-> URI mappings, and # initialize them with the default self._prem = {NS.XML_T: NS.XML} self._prem_r = {NS.XML: NS.XML_T} self._ids = {} self._refs = {} self._rules = rules
def get_timestamp(filename): class GMLHandler(xml.sax.ContentHandler): timestamp = None def startElement(self, name, attrs): if name == "wfs:FeatureCollection": self.timestamp = attrs['timeStamp'] handler = GMLHandler() parser = xml.sax.make_parser() parser.setContentHandler(handler) parser.parse(filename) timestamp = iso8601.parse_date(handler.timestamp, default_timezone=None) return pytz.timezone(settings.TIME_ZONE).localize(timestamp)
def _parseSOAP(xml_str, rules = None): try: from cStringIO import StringIO except ImportError: from StringIO import StringIO parser = xml.sax.make_parser() t = SOAPParser(rules = rules) parser.setContentHandler(t) e = xml.sax.handler.ErrorHandler() parser.setErrorHandler(e) inpsrc = xml.sax.xmlreader.InputSource() inpsrc.setByteStream(StringIO(xml_str)) # turn on namespace mangeling parser.setFeature(xml.sax.handler.feature_namespaces,1) parser.setFeature(xml.sax.handler.feature_external_ges, 0) try: parser.parse(inpsrc) except xml.sax.SAXParseException, e: parser._parser = None raise e return t ################################################################################ # SOAPParser's more public interface ################################################################################
def loadOsm(self, filename): if(not os.path.exists(filename)): print "No such data file %s" % filename return try: parser = make_parser() parser.setContentHandler(self) parser.parse(filename) except xml.sax._exceptions.SAXParseException: print "Error loading %s" % filename
def reset(self): self.pulldom = pulldom.SAX2DOM() # This content handler relies on namespace support self.parser.setFeature(xml.sax.handler.feature_namespaces, 1) self.parser.setContentHandler(self.pulldom)
def test_basic(self): """Ensure SAX2DOM can parse from a stream.""" with io.StringIO(SMALL_SAMPLE) as fin: sd = SAX2DOMTestHelper(fin, xml.sax.make_parser(), len(SMALL_SAMPLE)) for evt, node in sd: if evt == pulldom.START_ELEMENT and node.tagName == "html": break # Because the buffer is the same length as the XML, all the # nodes should have been parsed and added: self.assertGreater(len(node.childNodes), 0)
def main(): """ ?????????????????,???????????????ToolHireHandler????XML????parse()??. ???????,?????????????? :return: """ handler = ToolHireHandler() parser = xml.sax.make_parser() parser.setContentHandler(handler) parser.parse("toolhire.xml") print(handler.dates)
def _parseSOAP(xml_str, rules = None, ignore_ext=None, forbid_entities=False, forbid_external=True, forbid_dtd=False): inpsrc = xml.sax.xmlreader.InputSource() inpsrc.setByteStream(StringIO(xml_str)) if ignore_ext is None: ignore_ext = False parser = make_parser() t = SOAPParser(rules=rules) parser.setContentHandler(t) errorHandler = xml.sax.handler.ErrorHandler() parser.setErrorHandler(errorHandler) if ignore_ext: # disable by default entity loading on posted content forbid_dtd = True forbid_entities = True forbid_external = True parser.forbid_dtd = forbid_dtd parser.forbid_entities = forbid_entities parser.forbid_external = forbid_external parser.setEntityResolver(EmptyEntityResolver()) # turn on namespace mangeling parser.setFeature(xml.sax.handler.feature_namespaces, 1) try: parser.parse(inpsrc) except DefusedXmlException, e: parser._parser = None print traceback.format_exc() raise e except xml.sax.SAXParseException, e: parser._parser = None print traceback.format_exc() raise e return t ################################################################################ # SOAPParser's more public interface ################################################################################