def appendMessage(self, dom, message): historyElement = dom.getElementsByTagName(CommitHelperConstants.XMLTAGHISTORY)[0] item = dom.createElement(CommitHelperConstants.XMLTAGITEM) brief = dom.createElement(CommitHelperConstants.XMLTAGBRIEF) briefText = dom.createTextNode(message[0]) comment = dom.createElement(CommitHelperConstants.XMLTAGCOMMENT) commentText = dom.createTextNode(message[1]) initialFindings = dom.createElement(CommitHelperConstants.XMLTAGINITIALFINDINGS) initialFindingsText = dom.createTextNode(message[2]) reviewers = dom.createElement(CommitHelperConstants.XMLTAGREVIEWERS) reviewersText = dom.createTextNode(message[3]) reviewFindings = dom.createElement(CommitHelperConstants.XMLTAGREVIEWFINDINGS) reviewFindingsText = dom.createTextNode(message[4]) risk = dom.createElement(CommitHelperConstants.XMLTAGRISK) risk.setAttribute(CommitHelperConstants.XMLATTRIBUTEOPTION, message[5]) jirakey = dom.createElement(CommitHelperConstants.XMLTAGJIRAKEY) jirakeyText = dom.createTextNode(message[6]) staticCodeAnalysis = dom.createElement(CommitHelperConstants.XMLTAGSTATICCODEANALYSIS) staticCodeAnalysis.setAttribute(CommitHelperConstants.XMLATTRIBUTEOPTION, message[7]) staticCodeAnalysisText = dom.createTextNode(message[8]) brief.appendChild(briefText) comment.appendChild(commentText) initialFindings.appendChild(initialFindingsText) reviewers.appendChild(reviewersText) reviewFindings.appendChild(reviewFindingsText) jirakey.appendChild(jirakeyText) staticCodeAnalysis.appendChild(staticCodeAnalysisText) item.appendChild(brief) item.appendChild(comment) item.appendChild(initialFindings) item.appendChild(reviewers) item.appendChild(reviewFindings) item.appendChild(risk) item.appendChild(jirakey) item.appendChild(staticCodeAnalysis) historyElement.appendChild(item) items = historyElement.getElementsByTagName(CommitHelperConstants.XMLTAGITEM) while len(items) > CommitHelperConstants.MAXHISTORYSIZE: historyElement.removeChild(items[0]) items = historyElement.getElementsByTagName(CommitHelperConstants.XMLTAGITEM) homePath = os.path.expanduser('~') configPath = os.path.join(homePath, CommitHelperConstants.CONFIGDIR) configFile = os.path.join(configPath, CommitHelperConstants.CONFIGFILE) try: config = open(configFile, 'w') config.write(dom.toxml()) config.flush() config.close() except IOError: messagebox.showerror(CommitHelperConstants.ERROR, CommitHelperConstants.MSGERRORWRITINGFILE % configFile)
def CreateXml(): # =====从一个空xml文档开始 impl = xml.dom.getDOMImplementation() dom = impl.createDocument(None,'All_Students',None) root = dom.documentElement # --创建一个节点,并添加到root下 student = dom.createElement('student') root.appendChild(student) # --创建一个子节点,并设置属性 nameE = dom.createElement('name') value = u'陈奕迅' nameE.setAttribute("attr",value) nameN = dom.createTextNode(value) nameE.appendChild(nameN) student.appendChild(nameE) # -- 写进文件,如果出现了unicode,指定文件的编码 f = codecs.open('1.xml','w','utf-8') dom.writexml(f,addindent=' ',newl='\n',encoding = 'utf-8') f.close()
def add_string_element(dom, xmlnode, ns, name, data, attributes=()): if ns: e = dom.createElementNS(ns, name) else: e = dom.createElement(name) e.appendChild(dom.createTextNode(data)) for (attr_name, attr_value) in attributes: e.setAttribute(attr_name, attr_value) xmlnode.appendChild(e)
def create_text_element(dom, ns, name, data, attributes=()): if ns: e = dom.createElementNS(ns, name) else: e = dom.createElement(name) e.appendChild(dom.createTextNode(data)) for (attr_name, attr_value) in attributes: e.setAttribute(attr_name, attr_value) return e
from xml.dom import minidom as dom from xml.dom.ext import PrettyPrint doc = dom.Document() div = dom.createElement('div') text = dom.createTextNode('Python and XML') div.appendChild(text) doc.appendChild(div) PrettyPrint(doc)