• python 创建XML


    from xml.dom.minidom import Document
    
    # 创建Dom
    doc = Document()
    # 创建一个根节点
    root = doc.createElement("root")
    doc.appendChild(root)
    # 给根节点添加属性
    root.setAttribute("class", "三年二班")
    # 给根节点添加子节点
    datas = [
        {"name": "小明", "age": 10},
        {"name": "小红", "age": 11},
        {"name": "小刚", "age": 12},
    ]
    
    for data in datas:
        node = doc.createElement("student")
        node.setAttribute("age", str(data['age']))
        node.appendChild(doc.createTextNode(data["name"]))
        root.appendChild(node)
    # 给子节点添加子节点
    # 输出为字符串
    print(doc.toxml(encoding="utf-8"))
    # 输出为文件
    with open("aa.xml", "w", encoding="utf-8") as f:
        doc.writexml(f, encoding="utf-8", indent='	', addindent='	', newl='
    ')
    
  • 相关阅读:
    GITHUB常见命令
    GITHUB常用命令
    java构建简单的HTTP服务器
    是否会被锁
    GITHUB使用指南
    GITHUB使用指南、
    C#
    金蝶后台表对应
    金蝶K3表
    nodejs nodejs的操作
  • 原文地址:https://www.cnblogs.com/iFanLiwei/p/13597749.html
Copyright © 2020-2023  润新知