forked from GW-HIVE/scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathxml-parser.1.py
More file actions
66 lines (45 loc) · 1.62 KB
/
xml-parser.1.py
File metadata and controls
66 lines (45 loc) · 1.62 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
import os,sys
import string
from optparse import OptionParser
from lxml import etree
from io import StringIO
__version__="1.0"
__status__ = "Dev"
"""
This script will print(std out) some domains of a .xml file.
This script has three options, you can execute the script in three ways:
1. python xml-parser.1.py --version
This is the option that show you the program's version.
2. python xml-parser.1.py -h
This can show you some help information.
3. python xml-parser.1.py -i <filename.xml>
-i specifies input
This option will open the .xml file you input.
If the element tag is "book", it will output all the attributes of elements.
If the element tag is "title", it will output the text of the elements.
Usage:
python xml-parser.1.py -i <filename.xml>
"""
###############################
def main():
usage = "\n%prog [options]"
parser = OptionParser(usage,version="%prog " + __version__)
parser.add_option("-i","--xmlFile",action="store",dest="xmlFile",help="Input xml file")
(options,args) = parser.parse_args()
for file in ([options.xmlFile]):
if not (file):
parser.print_help()
sys.exit(0)
xmlFile = options.xmlFile
FR = open(xmlFile)
xml = FR.read()
FR.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()