-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathxml_parser.py
More file actions
71 lines (48 loc) · 1.53 KB
/
Copy pathxml_parser.py
File metadata and controls
71 lines (48 loc) · 1.53 KB
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
"""
This script will print(std out) some domains of a .xml file.
Input:
######
* -i : specified input file
Output:
#######
* Some domains of a .xml file
Usage:
#######
* python xml-parser.py --version
* This is the option that show you the program's version.*
* python xml-parser.py -h
* This can show you some help information.*
* python xml-parser.1.py -i <filename.xml>
*Runs program with specified input file*
"""
import sys
from io import StringIO
from argparse import ArgumentParser
from lxml import etree # pylint: disable=import-error
__version__ = "1.0"
__status__ = "Dev"
def main():
"""Finds and prints XML domains"""
usage = "\n%prog [options]"
parser = ArgumentParser(description=usage)
parser.add_argument('--version', '-V', action='version',
version="%(prog)s " + __version__)
parser.add_argument("-i", "--xmlFile", action="store", dest="xmlFile", help="Input xml file")
options = parser.parse_args()
for file in [options.xmlFile]:
if not file:
parser.print_help()
sys.exit(0)
xml_file = options.xmlFile
file_reader = open(xml_file)
xml = file_reader.read()
file_reader.close()
context = etree.iterparse(StringIO(xml), events=("start", "end"))
for action, elem in context:
if action == "start":
if elem.tag == "book":
print(elem.attrib["id"])
elif elem.tag == "title":
print(elem.text)
if __name__ == '__main__':
main()