2010-05-25 51 views
0

我有兩個xml文件添加一個節點: 1)model.xml 2)projectionParametersTemplate.xml[Python的]的Xml從另一個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

非常感謝。

回答

0

對我而言,它有效,例如xmlmodel的算法節點將從xmltemplate添加到ProjectionParameters節點。

我的猜測是你想改變實際的文件。使用你的代碼,只有內存中的對象被修改,而不是磁盤上的文件。當你提高你的接受率

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

P.S:也許你會得到更多的答案:如果你想改變的文件,在末尾加上這一行。

相關問題