2

我正在使用 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 标记闭包。我真的不知道为什么它保持文档打开。有没有人遇到过同样的问题?还是我只是忘记了一些非常明显的想法而我根本看不到问题?

4

2 回答 2

6

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

于 2011-08-18T09:15:30.730 回答
1

您的 XML 有效:查看:http ://en.wikipedia.org/wiki/XML_Schema_%28W3C%29

您不需要关闭声明。

于 2011-08-18T09:20:34.960 回答