-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfindElement.py
executable file
·42 lines (31 loc) · 920 Bytes
/
findElement.py
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
38
39
40
41
42
#!/usr/bin/python
from xml.sax import make_parser
from xml.sax.handler import ContentHandler
import string, sys
def trim(text):
return ' '.join(text.split())
class FindContent(ContentHandler):
def __init__(self, name):
self.toFind = name
self.inElement = 0
self.found = ''
def startElement(self, name, attrs):
self.found = ""
if name == self.toFind:
self.inElement = 1
else:
self.inElement = 0
def characters(self, ch):
if self.inElement:
self.found = self.found + ch
def endElement(self, name):
if name == self.toFind:
print(trim(self.found))
def find_elements(name):
handler = FindContent(name)
parser = make_parser()
parser.setContentHandler(handler)
parser.parse(open('films.xml'))
if __name__ == "__main__":
name = sys.argv[1]
find_elements(name)