Package diffpy :: Package srrietveld :: Package convert :: Module xmlparser
[frames] | no frames]

Source Code for Module diffpy.srrietveld.convert.xmlparser

  1  ############################################################################## 
  2  # 
  3  # diffpy.srrietveld by DANSE Diffraction group 
  4  #                   Simon J. L. Billinge 
  5  #                   (c) 2009 Trustees of the Columbia University 
  6  #                   in the City of New York.  All rights reserved. 
  7  # 
  8  # File coded by:    Peng Tian 
  9  # 
 10  # See AUTHORS.txt for a list of people who contributed. 
 11  # See LICENSE.txt for license information. 
 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   
21 -class XMLFile:
22 """ Parser XML file"""
23 - def __init__(self):
24 """ Initialization """ 25 return
26
27 - def importXMLFile(self, filename):
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
38 - def xmlToDict(self, xmlnode):
39 """ Convert an XML object to a dictionary """ 40 rdict = {} 41 # 1. Attribution 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 # 2. Recursive to call child nodes 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
67 -class StructureXML(XMLFile):
68 - def __init__(self, structurefilename):
69 self.structurefile = structurefilename 70 self.structurexml = self.importXMLFile(structurefilename) 71 return
72
73 - def parseStructureXML(self):
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
84 -class InstrumentXML(XMLFile):
85 - def __init__(self, instrumentfilename):
86 self.instrumentfile = instrumentfilename 87 self.instrumentxml = self.importXMLFile(instrumentfilename) 88 return
89
90 - def parseInstrumentXML(self):
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
101 -class StrategyXML(XMLFile):
102 - def __init__(self, strategyfilename):
103 self.strategyfile = strategyfilename 104 self.strategyxml = self.importXMLFile(strategyfilename) 105 return
106
107 -class BackgroundXML(XMLFile):
108 - def __init__(self, backgroundfilename):
109 self.backgroundfile = backgroundfilename 110 self.backgroundxml = self.importXMLFile(backgroundfilename) 111 return
112
113 - def addToInstDict(self, instdict):
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
122 -class ExcludedRegionXML(XMLFile):
123 - def __init__(self, excludedregionfilename):
124 self.excludedregionfile = excludedregionfilename 125 self.exrexml = self.importXMLFile(excludedregionfilename) 126 return
127