我在使用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标记闭包。 我真的不知道为什么要把文件打开。有人遇到过同样的问题吗?或者我只是忘记了一些很明显的事情而我根本看不到问题?在
<?xml ... ?>
不是标记,而是XML Declaration。不需要关闭它,您的文档处于完美状态。在您的XML有效: 看看:http://en.wikipedia.org/wiki/XML_Schema_%28W3C%29
您不需要关闭声明。在
相关问题 更多 >
编程相关推荐