Python Xml类
生活随笔
收集整理的這篇文章主要介紹了
Python Xml类
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
#-*- coding:utf-8 -*- from xml.dom import minidom, Node
import codecs
import sys
import osclass XmlUtility:# 常用工具類#def __init__():headNode = "configuration"listNode = "section"node = "setting"rootHead = ''hasCreateXml = Falsedoc = minidom.Document()def __init__(self, xmlPath):self.xmlFilePath = xmlPathdef CreateDefaultXml(self): #測XML配置文件是否存在 if (os.path.exists(self.xmlFilePath)):returnself.rootHead = self.doc.createElement(self.headNode)self.doc.appendChild(self.rootHead)#self.SaveXml()self.hasCreateXml = Truedef ReadXml(self,path):self.doc = minidom.parse(path)st = self.doc.firstChildwebsites = st.childNodeslstList = []for sw in websites: if sw.nodeType == sw.ELEMENT_NODE :lsty = []attributes = ''flag = Falsefor attribute in sw.attributes.keys():if(flag == False):attributes += attribute + "|" + sw.getAttribute(attribute)else:attributes += "^" + attribute + "|" + sw.getAttribute(attribute)flag = TruendName = sw.nodeName.replace("\n", "").replace("\t", "")ndValue = sw.firstChild.data.replace("\n", "").replace("\t", "")lsty.append([ndName + "|" + ndValue, attributes])for nd in sw.childNodes: if nd.nodeType == nd.ELEMENT_NODE:attributes = ''flag = Falsefor attribute in nd.attributes.keys():if(flag == False):attributes += attribute + "|" + nd.getAttribute(attribute)else:attributes += "^" + attribute + "|" + nd.getAttribute(attribute)flag = TruendName = nd.nodeName.replace("\n", "").replace("\t", "")ndValue = nd.firstChild.data.replace("\n", "").replace("\t", "")lsty.append([ndName + "|" + ndValue, attributes])lstList.append(lsty)return lstListdef InsertXml(self, listValues):#self.rootHead = self.doc.createElement(self.headNode)#self.doc.appendChild(self.rootHead)for i in range(0,len(listValues)):ss = listValues[i]valuess = ss[0][0].split('|')att = ss[0][1].split('^')root = self.doc.createElement(valuess[0])for k in range(0, len(att)):valuess = att[k].split('|')root.setAttribute(valuess[0], valuess[1])self.rootHead.appendChild(root)for j in range(1,len(ss)):values = ss[j][0].split('|')attributes = ss[j][1].split('^')nodes = self.doc.createElement(values[0])for k in range(0, len(attributes)):value = attributes[k].split('|')nodes.setAttribute(value[0], value[1]) nodes.appendChild(self.doc.createTextNode(values[1])) root.appendChild(nodes)self.SaveXml()def SaveXml(self):self.doc = minidom.parseString(self.doc.toprettyxml())f = file(self.xmlFilePath, 'w')writer = codecs.lookup('utf-8')[3](f)self.doc.writexml(writer, encoding='utf-8')writer.close()def SaveXmls(self, docs):docs = minidom.parseString(docs.toprettyxml())f = file(self.xmlFilePath, 'w')writer = codecs.lookup('utf-8')[3](f)doc.writexml(writer, encoding='utf-8')writer.close() def forMatList(self, lists):values = []values.append(["section|","sys|" + lists[0]])values.append(["JobID|" + lists[1],"id|JobID"])values.append(["JobName|" + lists[2],"id|JobName^value|" + lists[3]])values.append(["JobDescription|" + lists[4],"id|JobDescription"])values.append(["JobImpact|" + lists[4],"id|JobImpact"])return valuesp = SendMail()
p.InitXml(filePath)lists = []lists.append(p.forMatList(["WebOrder","JOB_OE_OrderFinishToW101","1","True","DSD/101/WEB101訂單生成是否正常"]))
lists.append(p.forMatList(["WebOrder","JOB_OE_OrderFinishToW101E","2","True","DSD/101/WEB101訂單生成是否正常"]))
lists.append(p.forMatList(["WebOrder","JOB_OE_MCMOrderToOrderFinish","3","True","DSD/101/WEB101訂單生成是否正常"]))if(p.xml.hasCreateXml):p.xml.InsertXml(lists)lists = p.xml.ReadXml(filePath)
?
?
轉載于:https://www.cnblogs.com/xsmhero/archive/2012/12/04/2801266.html
《新程序員》:云原生和全面數字化實踐50位技術專家共同創(chuàng)作,文字、視頻、音頻交互閱讀總結
以上是生活随笔為你收集整理的Python Xml类的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: ubuntu 用户配置文件设置
- 下一篇: 前天在一直Linux根文件系统时提示WA