代码之家  ›  专栏  ›  技术社区  ›  michele

在python中修改XML文件

  •  1
  • michele  · 技术社区  · 14 年前

    我有两个文件,文件1和文件2。 我必须在一个特定的节点中修改file1并添加一个子节点列表。 列表在文件2中。 我能做吗?怎么做?

    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 回复  |  直到 13 年前
        1
  •  3
  •   johntellsall    14 年前

    使用元素树:

    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