python minidom 不关闭 <xml 标签>

标签 python xml 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 标记关闭。 我真的不知道为什么要让文档保持打开状态。有没有人遇到过同样的问题?还是我只是忘记了一些非常明显的事情而我根本看不到问题?

最佳答案

<?xml ... ?>不是标签,而是 XML Declaration .无需关闭它,您的文档完好无损。

关于python minidom 不关闭 <xml 标签>,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7104997/

相关文章:

python - 自然排序

c# - XML Unicode 安全编码

Python - 使用 minidom 解析时理解 XML 结构

两个或多个线程之间的 Python 关系/通信

python - Python中使用print输出对象

python - 如何使用数据库训练智能聊天机器人,然后添加到 iOS 应用程序

android - 使简单的 Accordion 可滚动

java - 如何让状态栏全白?

python - 哪个 XML 解析器具有最易读的错误报告?

python - 无法在类内调用 xml.dom.minidom.parse