在Python中修改XML文件
我有两个文件,file1 和 file2。
我需要在 file1 的一个特定位置修改内容,并添加一个子节点列表。
这个子节点列表在 file2 里。
我可以这样做吗?怎么做呢?
from xml.dom.minidom import Document
from xml.dom import minidom
file1=modificare.xml
file2=sorgente.xml
xmldoc=minidom.parse(file1)
for Node in xmldoc.getElementsByTagName("Sampler"):
# put in the file2 content
1 个回答
3
使用 ElementTree:
from xml.etree.ElementTree import Element, SubElement, Comment, tostring
# Configure one attribute with set()
root = Element('opml')
root.set('version', '1.0')
root.append(Comment('Generated by ElementTree_csv_to_xml.py for PyMOTW'))
http://broadcast.oreilly.com/2010/03/pymotw-creating-xml-documents.html