日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

Python Xml类

發布時間:2025/7/14 python 22 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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位技術專家共同創作,文字、視頻、音頻交互閱讀

總結

以上是生活随笔為你收集整理的Python Xml类的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。