1
2
3
4
5
6
7
8
9
10
11
12
13
14
15 """ XMLfile parser is to parse XML input files. """
16
17 __id__ = "$Id: xmlparser.py 5658 2010-07-09 20:05:34Z juhas $"
18
19 import xml.dom.minidom
20
22 """ Parser XML file"""
24 """ Initialization """
25 return
26
28 """ Import general XML file """
29 try:
30 xmlfile = open(filename, "r")
31 rietxml = xml.dom.minidom.parse(xmlfile)
32 xmlfile.close()
33 except IOError:
34 errmsg = "%-10s Cannot Be Found" % (filename)
35 raise IOError(errmsg)
36 return rietxml
37
39 """ Convert an XML object to a dictionary """
40 rdict = {}
41
42 if xmlnode.attributes is not None:
43 for index in range(xmlnode.attributes.length):
44 attrname = xmlnode.attributes.item(index).name
45 attrval = xmlnode.attributes.getNamedItem(attrname).value
46 try:
47 rdict[str(attrname)] = int(attrval)
48 continue
49 except:
50 try:
51 rdict[str(attrname)] = float(attrval)
52 continue
53 except:
54 rdict[str(attrname)] = str(attrval)
55
56
57
58 for child in xmlnode.childNodes:
59 if child == None:
60 continue
61 if not rdict.has_key(str(child.localName)):
62 rdict[str(child.localName)] = []
63 rdict[str(child.localName)].append( self.xmlToDict(child) )
64
65 return rdict
66
72
74 for childnode in self.structurexml.childNodes:
75 if childnode.localName == "MODEL":
76 modelnode = childnode
77 break
78 if childnode.localName != "MODEL" or modelnode == None:
79 raise NotImplementedError("Not recognized xml format!")
80
81 self.strudict = self.xmlToDict(modelnode)
82 return self.strudict
83
86 self.instrumentfile = instrumentfilename
87 self.instrumentxml = self.importXMLFile(instrumentfilename)
88 return
89
91 for childnode in self.instrumentxml.childNodes:
92 if childnode.localName == "INSTRUMENT":
93 instrumentnode = childnode
94 break
95 if childnode.localName != "INSTRUMENT" or instrumentnode == None:
96 raise NotImplementedError("Not recognized xml format!")
97
98 self.instdict = self.xmlToDict(instrumentnode)
99 return self.instdict
100
106
108 - def __init__(self, backgroundfilename):
109 self.backgroundfile = backgroundfilename
110 self.backgroundxml = self.importXMLFile(backgroundfilename)
111 return
112
114 instdict["BACKGROUND"] = []
115 for childnode in self.backgroundxml.childNodes:
116 if childnode.localName == "BACKGROUND":
117 instdict["BACKGROUND"].append(self.xmlToDict(childnode))
118 else:
119 raise NotImplementedError("Not recognized xml format!")
120 return instdict
121
123 - def __init__(self, excludedregionfilename):
124 self.excludedregionfile = excludedregionfilename
125 self.exrexml = self.importXMLFile(excludedregionfilename)
126 return
127