In our project we had kept the
operations rules as an XML and the below mentioned parser was used to
get the conditions to be applied for each operation.
The xml.dom.minidom was used to parse
the xml file and return a document object .The document object was
used to obtain the child nodes and for each child nodes the
corresponding fileds and attributes were retrieved using the get
attribute method and appended as a tuple.
The tuple was then included in the
dictionary with the nodename as the key.
import xml.dom.minidom class XMLParser: def __init__(self, filePath): self.xml_file_path = filePath def get_a_document(self): return xml.dom.minidom.parse(self.xml_file_path) def process_xml_return_dict(self): doc = self.get_a_document() fieldMapping = doc.childNodes[0] operations = {} for layer in fieldMapping.childNodes: if layer.nodeType == xml.dom.minidom.Node.TEXT_NODE or layer.nodeType == xml.dom.minidom.Node.COMMENT_NODE: continue fieldList = self.get_fields(layer) attrList = self.get_attributes(layer) operations[layer.nodeName] = (attrList, fieldList) return operations def get_attributes(self, node): attrList = [] nodeMap = node.attributes for index in range(nodeMap.length): attrName = nodeMap.item(index).name attrValue = node.getAttribute(attrName) attrList.append((attrName, attrValue)) return attrList def get_fields(self, node): fieldList = [] for childNode in node.childNodes: if childNode.nodeType != xml.dom.minidom.Node.TEXT_NODE and childNode.nodeType != xml.dom.minidom.Node.COMMENT_NODE: fromField = childNode.getAttribute('FromField') toField = childNode.getAttribute('ToField') fieldValue = childNode.getAttribute('Value') condition = childNode.getAttribute('Condition') fieldList.append((fromField, toField, fieldValue, condition)) return fieldList Python pickle module & Zip file creation Description The solution gives a solution to use the pickle module and to create a Zip File. # below is a typical Python dictionary object of roman numerals romanD1 = {'I':1,'II':2,'III':3,'IV':4,'V':5,'VI':6,'VII':7,'VIII':8,'IX':9,'X':10} # to save a Python object like a dictionary to a file # and load it back intact you have to use the pickle module import pickle print "The original dictionary:" print romanD1 file = open("roman1.dat", "w") pickle.dump(romanD1, file) file.close() # now load the dictionay object back from the file ... file = open("roman1.dat", "r") romanD2 = pickle.load(file) file.close() print "Dictionary after pickle.dump() and pickle.load():" print romanD2 # for large text files you can write and read a zipped file (PKZIP format) # notice that the syntax is mildly different from normal file read/write import zipfile zfilename = "English101.zip" zout = zipfile.ZipFile(zfilename, "w") zout.writestr(zfilename, str1 + str2) zout.close() # read the zipped file back in zin = zipfile.ZipFile(zfilename, "r") strz = zin.read(zfilename) zin.close() print "Testing the contents of %s:" % zfilename print strz