【问题标题】:[Python]Xml add a node from another xml document[Python]Xml从另一个xml文档添加一个节点
【发布时间】:2010-05-25 14:38:13
【问题描述】:

我有两个 xml 文件: 1)模型.xml 2)projectionParametersTemplate.xml

我想和他的孩子从1)算法节点中提取并放入2)

我已经写了这段代码,但它不起作用。

from xml.dom.minidom import Document
from xml.dom import minidom      
xmlmodel=minidom.parse("/home/michele/Scrivania/d/model.xml")
xmltemplate=minidom.parse("/home/michele/Scrivania/d/projectionParametersTemplate.xml")

for Node in xmlmodel.getElementsByTagName("Algorithm"):
     print "\nNode: "+str(Node)
     for Node2 in xmltemplate.getElementsByTagName("ProjectionParameters"):
          print "\nNode2: "+str(Node2)
          Node2.appendChild(Node)

这是model.xml link text

这是 projectionParametersTemplate.xml link text

非常感谢。

【问题讨论】:

    标签: python xml add


    【解决方案1】:

    对我来说它有效,例如来自 xmlmodel 的算法节点被添加到来自 xmltemplate 的 ProjectionParameters 节点。

    我的猜测是您想更改实际文件。使用您的代码,仅修改内存中的对象,而不修改磁盘上的文件。如果要更改文件,请在末尾添加此行:

    xmltemplate.writexml(file("PATH_TO_OUTPUT_FILE.xml","w"))
    

    P.S.:当你提高接受率时,也许你会得到更多的答案。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-04-10
      • 1970-01-01
      • 2018-08-31
      • 2014-12-22
      • 1970-01-01
      • 2011-03-03
      • 2013-02-22
      • 2011-03-03
      相关资源
      最近更新 更多