Python xml.sax.handler() Examples

The following are 27 code examples of xml.sax.handler(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module xml.sax , or try the search function .
Example #1
Source File: rss.py    From pycopia with Apache License 2.0 6 votes vote down vote up
def get_parser(document=None, namespaces=1, validate=0, external_ges=1,
        logfile=None, doc_factory=new_document):
    import xml
    if hasattr(xml, "use_pyxml"):
        xml.use_pyxml()
    import xml.sax.sax2exts
    import xml.sax.handler
    handler = POM.ContentHandler(document, doc_factory=doc_factory, logfile=logfile)
    errorhandler = POM.ErrorHandler(logfile)
    # create parser
    parser = xml.sax.sax2exts.XMLParserFactory.make_parser()
    parser.setFeature(xml.sax.handler.feature_namespaces, namespaces)
    parser.setFeature(xml.sax.handler.feature_validation, validate)
    parser.setFeature(xml.sax.handler.feature_external_ges, external_ges)
    parser.setFeature(xml.sax.handler.feature_external_pes, 0)
    parser.setFeature(xml.sax.handler.feature_string_interning, 1)
    # set handlers
    parser.setContentHandler(handler)
    parser.setDTDHandler(handler)
    parser.setEntityResolver(handler)
    parser.setErrorHandler(errorhandler)
    return parser 
Example #2
Source File: pulldom.py    From Splunking-Crime with GNU Affero General Public License v3.0 5 votes vote down vote up
def reset(self):
        self.pulldom = PullDOM()
        # This content handler relies on namespace support
        self.parser.setFeature(xml.sax.handler.feature_namespaces, 1)
        self.parser.setContentHandler(self.pulldom) 
Example #3
Source File: AimlParser.py    From py3Aiml_Chinese with MIT License 5 votes vote down vote up
def create_parser():
    """创建并返回一个 AIML 解析器对象。"""
    parser = xml.sax.make_parser()
    handler = AimlHandler("UTF-8")
    parser.setContentHandler(handler)
    #parser.setFeature(xml.sax.handler.feature_namespaces, True)
    return parser 
Example #4
Source File: test_xml_sax_jy.py    From CTFCrackTools with GNU General Public License v3.0 5 votes vote down vote up
def test_javasax_with_skipEntity(self):
        content = '<!DOCTYPE Message [<!ENTITY xxe SYSTEM "http://aws.amazon.com/">]><Message>error:&xxe;</Message>'

        root = RootElement()
        handler = XmlHandler(root, root)
        parser = xml.sax.make_parser()
        parser.setContentHandler(handler)
        parser.setFeature(xml.sax.handler.feature_external_ges, 0)
        parser.parse(StringIO(content))

        self.assertEqual('Message', root.start_elements[0][0])
        self.assertItemsEqual([['Message', 'error:', root]], root.end_elements) 
Example #5
Source File: pulldom.py    From CTFCrackTools with GNU General Public License v3.0 5 votes vote down vote up
def reset(self):
        self.pulldom = PullDOM()
        # This content handler relies on namespace support
        self.parser.setFeature(xml.sax.handler.feature_namespaces, 1)
        self.parser.setContentHandler(self.pulldom) 
Example #6
Source File: pulldom.py    From canape with GNU General Public License v3.0 5 votes vote down vote up
def reset(self):
        self.pulldom = PullDOM()
        # This content handler relies on namespace support
        self.parser.setFeature(xml.sax.handler.feature_namespaces, 1)
        self.parser.setContentHandler(self.pulldom) 
Example #7
Source File: pulldom.py    From android_universal with MIT License 5 votes vote down vote up
def reset(self):
        self.pulldom = PullDOM()
        # This content handler relies on namespace support
        self.parser.setFeature(xml.sax.handler.feature_namespaces, 1)
        self.parser.setContentHandler(self.pulldom) 
Example #8
Source File: pulldom.py    From unity-python with MIT License 5 votes vote down vote up
def reset(self):
        self.pulldom = PullDOM()
        # This content handler relies on namespace support
        self.parser.setFeature(xml.sax.handler.feature_namespaces, 1)
        self.parser.setContentHandler(self.pulldom) 
Example #9
Source File: pulldom.py    From PokemonGo-DesktopMap with MIT License 5 votes vote down vote up
def reset(self):
        self.pulldom = PullDOM()
        # This content handler relies on namespace support
        self.parser.setFeature(xml.sax.handler.feature_namespaces, 1)
        self.parser.setContentHandler(self.pulldom) 
Example #10
Source File: pulldom.py    From RevitBatchProcessor with GNU General Public License v3.0 5 votes vote down vote up
def reset(self):
        self.pulldom = PullDOM()
        # This content handler relies on namespace support
        self.parser.setFeature(xml.sax.handler.feature_namespaces, 1)
        self.parser.setContentHandler(self.pulldom) 
Example #11
Source File: test_xml_sax_jy.py    From CTFCrackTools-V2 with GNU General Public License v3.0 5 votes vote down vote up
def test_javasax_with_skipEntity(self):
        content = '<!DOCTYPE Message [<!ENTITY xxe SYSTEM "http://aws.amazon.com/">]><Message>error:&xxe;</Message>'

        root = RootElement()
        handler = XmlHandler(root, root)
        parser = xml.sax.make_parser()
        parser.setContentHandler(handler)
        parser.setFeature(xml.sax.handler.feature_external_ges, 0)
        parser.parse(StringIO(content))

        self.assertEqual('Message', root.start_elements[0][0])
        self.assertItemsEqual([['Message', 'error:', root]], root.end_elements) 
Example #12
Source File: pulldom.py    From CTFCrackTools-V2 with GNU General Public License v3.0 5 votes vote down vote up
def reset(self):
        self.pulldom = PullDOM()
        # This content handler relies on namespace support
        self.parser.setFeature(xml.sax.handler.feature_namespaces, 1)
        self.parser.setContentHandler(self.pulldom) 
Example #13
Source File: pulldom.py    From medicare-demo with Apache License 2.0 5 votes vote down vote up
def reset(self):
        self.pulldom = PullDOM()
        # This content handler relies on namespace support
        self.parser.setFeature(xml.sax.handler.feature_namespaces, 1)
        self.parser.setContentHandler(self.pulldom) 
Example #14
Source File: pulldom.py    From Project-New-Reign---Nemesis-Main with GNU General Public License v3.0 5 votes vote down vote up
def reset(self):
        self.pulldom = PullDOM()
        # This content handler relies on namespace support
        self.parser.setFeature(xml.sax.handler.feature_namespaces, 1)
        self.parser.setContentHandler(self.pulldom) 
Example #15
Source File: check_xml.py    From pre-commit-hooks with MIT License 5 votes vote down vote up
def main(argv: Optional[Sequence[str]] = None) -> int:
    parser = argparse.ArgumentParser()
    parser.add_argument('filenames', nargs='*', help='XML filenames to check.')
    args = parser.parse_args(argv)

    retval = 0
    handler = xml.sax.handler.ContentHandler()
    for filename in args.filenames:
        try:
            with open(filename, 'rb') as xml_file:
                xml.sax.parse(xml_file, handler)
        except xml.sax.SAXException as exc:
            print(f'{filename}: Failed to xml parse ({exc})')
            retval = 1
    return retval 
Example #16
Source File: pulldom.py    From jawfish with MIT License 5 votes vote down vote up
def reset(self):
        self.pulldom = PullDOM()
        # This content handler relies on namespace support
        self.parser.setFeature(xml.sax.handler.feature_namespaces, 1)
        self.parser.setContentHandler(self.pulldom) 
Example #17
Source File: pulldom.py    From ironpython3 with Apache License 2.0 5 votes vote down vote up
def reset(self):
        self.pulldom = PullDOM()
        # This content handler relies on namespace support
        self.parser.setFeature(xml.sax.handler.feature_namespaces, 1)
        self.parser.setContentHandler(self.pulldom) 
Example #18
Source File: pulldom.py    From Imogen with MIT License 5 votes vote down vote up
def reset(self):
        self.pulldom = PullDOM()
        # This content handler relies on namespace support
        self.parser.setFeature(xml.sax.handler.feature_namespaces, 1)
        self.parser.setContentHandler(self.pulldom) 
Example #19
Source File: pulldom.py    From Fluid-Designer with GNU General Public License v3.0 5 votes vote down vote up
def reset(self):
        self.pulldom = PullDOM()
        # This content handler relies on namespace support
        self.parser.setFeature(xml.sax.handler.feature_namespaces, 1)
        self.parser.setContentHandler(self.pulldom) 
Example #20
Source File: nmap.py    From pycopia with Apache License 2.0 5 votes vote down vote up
def get_parser(logfile=None, namespaces=0, validate=0, external_ges=0):
    handler = ContentHandler()
    # create parser
    parser = xml.sax.sax2exts.XMLParserFactory.make_parser()
    parser.setFeature(xml.sax.handler.feature_namespaces, namespaces)
    parser.setFeature(xml.sax.handler.feature_validation, validate)
    parser.setFeature(xml.sax.handler.feature_external_ges, external_ges)
    parser.setFeature(xml.sax.handler.feature_external_pes, 0)
    parser.setFeature(xml.sax.handler.feature_string_interning, 1)
    # set handlers
    parser.setContentHandler(handler)
    parser.setDTDHandler(handler)
    parser.setEntityResolver(handler)
    parser.setErrorHandler(ErrorHandler(logfile))
    return parser 
Example #21
Source File: POMparse.py    From pycopia with Apache License 2.0 5 votes vote down vote up
def get_parser(document=None, namespaces=0, validate=0, external_ges=1,
        logfile=None, doc_factory=POM.new_document):
    import xml
    if hasattr(xml, "use_pyxml"):
        xml.use_pyxml()
    import xml.sax.sax2exts
    import xml.sax.handler
    import new
    handler = ContentHandler(document, doc_factory=doc_factory, logfile=logfile)
    errorhandler = ErrorHandler(logfile)
    # create parser
    parser = xml.sax.sax2exts.XMLParserFactory.make_parser()
    parser.setFeature(xml.sax.handler.feature_namespaces, namespaces)
    parser.setFeature(xml.sax.handler.feature_validation, validate)
    parser.setFeature(xml.sax.handler.feature_external_ges, external_ges)
    parser.setFeature(xml.sax.handler.feature_external_pes, 0)
    parser.setFeature(xml.sax.handler.feature_string_interning, 1)
    # set handlers
    parser.setContentHandler(handler)
    parser.setDTDHandler(handler)
    parser.setEntityResolver(handler)
    parser.setErrorHandler(errorhandler)
    # since the xml API provides some generic parser I can't just
    # subclass I have to "patch" the object in-place with this trick.
    # This is to a) make the API compatible with the HTMLParser, and b)
    # allow specifing the encoding and other headers in the request.
    parser.parse_orig = parser.parse
    def parse(self, url, data=None, encoding=POM.DEFAULT_ENCODING,
                                    useragent=None, accept=None):
        from pycopia.WWW import urllibplus
        fo = urllibplus.urlopen(url, data, encoding, useragent=useragent, accept=accept)
        if logfile:
            from pycopia import UserFile
            fo = UserFile.FileWrapper(fo, logfile=logfile)
        return self.parse_orig(fo)
    parser.parse = new.instancemethod(parse, parser, parser.__class__)
    return parser 
Example #22
Source File: pulldom.py    From pmatic with GNU General Public License v2.0 5 votes vote down vote up
def reset(self):
        self.pulldom = PullDOM()
        # This content handler relies on namespace support
        self.parser.setFeature(xml.sax.handler.feature_namespaces, 1)
        self.parser.setContentHandler(self.pulldom) 
Example #23
Source File: pulldom.py    From oss-ftp with MIT License 5 votes vote down vote up
def reset(self):
        self.pulldom = PullDOM()
        # This content handler relies on namespace support
        self.parser.setFeature(xml.sax.handler.feature_namespaces, 1)
        self.parser.setContentHandler(self.pulldom) 
Example #24
Source File: pulldom.py    From Computable with MIT License 5 votes vote down vote up
def reset(self):
        self.pulldom = PullDOM()
        # This content handler relies on namespace support
        self.parser.setFeature(xml.sax.handler.feature_namespaces, 1)
        self.parser.setContentHandler(self.pulldom) 
Example #25
Source File: pulldom.py    From BinderFilter with MIT License 5 votes vote down vote up
def reset(self):
        self.pulldom = PullDOM()
        # This content handler relies on namespace support
        self.parser.setFeature(xml.sax.handler.feature_namespaces, 1)
        self.parser.setContentHandler(self.pulldom) 
Example #26
Source File: pulldom.py    From ironpython2 with Apache License 2.0 5 votes vote down vote up
def reset(self):
        self.pulldom = PullDOM()
        # This content handler relies on namespace support
        self.parser.setFeature(xml.sax.handler.feature_namespaces, 1)
        self.parser.setContentHandler(self.pulldom) 
Example #27
Source File: pulldom.py    From meddle with MIT License 5 votes vote down vote up
def reset(self):
        self.pulldom = PullDOM()
        # This content handler relies on namespace support
        self.parser.setFeature(xml.sax.handler.feature_namespaces, 1)
        self.parser.setContentHandler(self.pulldom)