#-*- coding:utf-8 -*-
from xml.dom import minidom, Node
import codecs
import sys
import os
class XmlUtility:
# 常用工具类
#def __init__():
headNode = "configuration"
listNode = "section"
node = "setting"
rootHead = ''
hasCreateXml = False
doc = minidom.Document()
def __init__(self, xmlPath):
self.xmlFilePath = xmlPath
def CreateDefaultXml(self):
#测XML配置文件是否存在
if (os.path.exists(self.xmlFilePath)):
return
self.rootHead = self.doc.createElement(self.headNode)
self.doc.appendChild(self.rootHead)
#self.SaveXml()
self.hasCreateXml = True
def ReadXml(self,path):
self.doc = minidom.parse(path)
st = self.doc.firstChild
websites = st.childNodes
lstList = []
for sw in websites:
if sw.nodeType == sw.ELEMENT_NODE :
lsty = []
attributes = ''
flag = False
for attribute in sw.attributes.keys():
if(flag == False):
attributes += attribute + "|" + sw.getAttribute(attribute)
else:
attributes += "^" + attribute + "|" + sw.getAttribute(attribute)
flag = True
ndName = 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 = False
for attribute in nd.attributes.keys():
if(flag == False):
attributes += attribute + "|" + nd.getAttribute(attribute)
else:
attributes += "^" + attribute + "|" + nd.getAttribute(attribute)
flag = True
ndName = nd.nodeName.replace("\n", "").replace("\t", "")
ndValue = nd.firstChild.data.replace("\n", "").replace("\t", "")
lsty.append([ndName + "|" + ndValue, attributes])
lstList.append(lsty)
return lstList
def 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 values
p = 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)