import xml.etree.ElementTree as ET #读取xml文件 class ConfigUtils(): def __init__(self,file): self.url = "" self.tree = ET.parse(file) self.root = self.tree.getroot() def readTop(self, key, child): datas = [] data = self.root.find(key) for key in child: datas.append(data.get(key)) return datas def readTopDict(self, key, child): datas = {} data = self.root.find(key) for key in child: datas[key] = data.get(key) return datas def readConfig(self,parent,child): datas=[] for childLine in self.root.find(parent): data=[] for key in child: data.append(childLine.get(key)) datas.append(data) return datas def readConfigDict(self,parent,child): datas=[] for childLine in self.root.find(parent): data={} for key in child: data[key]=childLine.get(key) datas.append(data) return datas def readConfigSingle(self,key): data = self.root.find(key).text return data if __name__ == '__main__': config = ConfigUtils("logger.conf")