Python:使用 XML DOM 将子项附加到已创建的 XML 文件的根目录

Python: Appending children to an already created XML file's root using XML DOM

我是 XML 的新手,我一直在研究如何使用 Python 和 [=30 将子项附加到已经存在的 XML 文件的根节点=] DOM。现在我有这个脚本来创建一个输出文件:

from xml.dom.minidom import Document

doc = Document()

root_node = doc.createElement("notes")                          # Root
doc.appendChild(root_node)

object_node = doc.createElement("data")                         # Child
root_node.appendChild(object_node)

object_node.setAttribute("a_version", "something_v001.0001.ma") # Set attributes
object_node.setAttribute("b_user", "Me")
object_node.setAttribute("c_comment", "comment about file")

xml_file = open("C:/Temp/file_notes.xml", "w")                  # Append
xml_file.write(doc.toprettyxml())
xml_file.close()

这给了我一个看起来像这样的输出文件:

<?xml version="1.0" ?>
<notes>
    <data a_version="Me" b_user="something_v001.0001.ma" c_comment="comment about file"/>
</notes>

我想将未来的数据追加到此文件中,以便在添加 2 个版本后看起来像这样:

<?xml version="1.0" ?>
<notes>
    <data a_version="something_v001.0001.ma" b_user="Me" c_comment="comment about file"/>
    <data a_version="something_v001.0002.ma" b_user="You" c_comment="minor save"/>
    <data a_version="something_v002.0003.ma" b_user="Them" c_comment="major save"/>
</notes>

但是我每次附加数据的尝试都是这样的:

<?xml version="1.0" ?>
<notes>
    <data a_version="Me" b_user="something_v001.0001.ma" c_comment="comment about file"/>
</notes>
<?xml version="1.0" ?>
<notes>
    <data a_version="Me" b_user="something_v001.0001.ma" c_comment="comment about file"/>
</notes>
<?xml version="1.0" ?>
<notes>
    <data a_version="Me" b_user="something_v001.0001.ma" c_comment="comment about file"/>
</notes>

如果有人有任何替代方法来使用 ElementTree 完成此任务,我们也将不胜感激。资源好像多了很多,但是我不知道怎么用Maya来实现这个解决方案。谢谢!

您没有向我们展示任何演示 "every attempt I make at appending data" 的代码。但没关系,下面是如何使用 ElementTree 将新元素附加到现有 XML 文件的方法。

from xml.etree import ElementTree as ET
from xml.dom import minidom

# Assume that we have an existing XML document with one "data" child
doc = ET.parse("file_notes.xml")
root = doc.getroot()

# Create 2 new "data" elements
data1 = ET.Element("data", {"a_version": "something_v001.0002.ma",
                            "b_user": "You",
                            "c_comment": "minor save"})
data2 = ET.Element("data", {"a_version": "something_v001.0003.ma",
                            "b_user": "Them",
                            "c_comment": "major save"})

# Append the new "data" elements to the root element of the XML document
root.append(data1)
root.append(data2)

# Now we have a new well-formed XML document. It is not very nicely formatted...
out = ET.tostring(root)

# ...so we'll use minidom to make the output a little prettier
dom = minidom.parseString(out)
print dom.toprettyxml()

输出:

<?xml version="1.0" ?>
<notes>


    <data a_version="Me" b_user="something_v001.0001.ma" c_comment="comment about file"/>


    <data a_version="something_v001.0002.ma" b_user="You" c_comment="minor save"/>
    <data a_version="something_v001.0003.ma" b_user="Them" c_comment="major save"/>
</notes>

ElementTree 没有内置的漂亮打印机,因此我们使用 minidom。输出包含一些多余的空格,但比 ElementTree 提供的要好。