python minidom不关闭<xml tag>

2024-09-27 20:17:26 发布

您现在位置:Python中文网/ 问答频道 /正文

我在使用minidom时遇到了一种奇怪的行为。我运行以下代码:

import os
import sys
from xml.dom import minidom
def generateReleaseXMLFile():
    modelPath = "%./model/"
    # Create the parser
    xsydoc  = minidom.Document()
    # Create the element ScriptModelVersion
    scriptModelVersion  = xsydoc.createElement('ScriptModelVersion')
    # Assign all the attributes
    scriptModelVersion.setAttribute("Major", "1")
    scriptModelVersion.setAttribute("Minor", "2")
    scriptModelVersion.setAttribute("Patch", "3")
    scriptModelVersion.setAttribute("ReseaseDate", "2011-05-20")
    # append the root to the document
    xsydoc.appendChild(scriptModelVersion)
    # Create the file descriptor
    fdesc = open(modelPath+"Release.xml", "w")
    # Write the file
    fdesc.write(xsydoc.toprettyxml())
    # Close the file
    fdesc.close()
    print xsydoc.toprettyxml()

generateReleaseXMLFile()

它生成以下输出:

^{pr2}$

没有xml标记闭包。 我真的不知道为什么要把文件打开。有人遇到过同样的问题吗?或者我只是忘记了一些很明显的事情而我根本看不到问题?在


Tags: the代码importcreatexmlfileminidomsetattribute

热门问题