开发者

Modify an XML file in Python

开发者 https://www.devze.com 2022-12-31 00:23 出处:网络
I have two files, file1 and file2. I have to modify file1 in a particular nodeand add in a list of children.

I have two files, file1 and file2. I have to modify file1 in a particular node and add in a list of children. The list is in file2. Can I do it, and how?

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 conte开发者_StackOverflow社区nt


use 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

0

精彩评论

暂无评论...
验证码 换一张
取 消