File: xml_sax.py

package info (click to toggle)
bandit 1.6.2-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 1,824 kB
  • sloc: python: 10,049; makefile: 22; sh: 14
file content (37 lines) | stat: -rw-r--r-- 1,043 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
import xml.sax
from xml import sax
import defusedxml.sax

class ExampleContentHandler(xml.sax.ContentHandler):
    def __init__(self):
        xml.sax.ContentHandler.__init__(self)

    def startElement(self, name, attrs):
        print('start:', name)

    def endElement(self, name):
        print('end:', name)

    def characters(self, content):
        print('chars:', content)

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()

if __name__ == "__main__":
    main()