python minidom 不关闭
我在使用 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()
它生成以下输出:
<?xml version="1.0" ?>
<ScriptModelVersion Major="9" Minor="0" Patch="1" ReleaseDate="2011-05-20"/>
没有 xml 标签闭合。 我真的不知道为什么它保持文档打开。有人遇到过同样的问题吗?或者我只是忘记了一些非常明显的事情而我根本看不到问题?
I'm experiencing a strange behaviour using minidom. I run the following code:
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()
It generates the following output:
<?xml version="1.0" ?>
<ScriptModelVersion Major="9" Minor="0" Patch="1" ReleaseDate="2011-05-20"/>
whithout the xml tag closure.
I really have no idea about why it's keeping the document open. Did anyone experienced the same problem? Or am i simply forgetting somethink really obvious and i simply canot see the problem?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
不是标记,而是 XML 声明。无需关闭它,您的文档就处于完美状态。
The
<?xml ... ?>
is not a tag, but the XML Declaration. There is not need to close it, your document is in perfect shape.您的 XML 有效:
看看:http://en.wikipedia.org/wiki/XML_Schema_%28W3C% 29
您不需要关闭声明。
Your XML is valid :
Have a look at : http://en.wikipedia.org/wiki/XML_Schema_%28W3C%29
You do not need to close declaration.