python minidom 不关闭

发布于 2024-11-30 02:45:22 字数 1137 浏览 2 评论 0原文

我在使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

骄傲 2024-12-07 02:45:22

不是标记,而是 XML 声明。无需关闭它,您的文档就处于完美状态。

The <?xml ... ?> is not a tag, but the XML Declaration. There is not need to close it, your document is in perfect shape.

临风闻羌笛 2024-12-07 02:45:22

您的 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文