目 录CONTENT

文章目录

python创建xml文件

懿曲折扇情
2022-11-08 / 0 评论 / 3 点赞 / 32 阅读 / 281 字 / 正在检测是否收录...
温馨提示:
本文最后更新于 2022-11-08,若内容或图片失效,请留言反馈。部分素材来自网络,若不小心影响到您的利益,请联系我们删除。
广告 广告

代码

# coding=utf-8
"""
    @project: automation_tools
    @Author:gaojs
    @file: test013.py
    @date:2022/11/8 10:03
    @blogs: https://www.gaojs.com.cn
"""

from xml.dom.minidom import Document

# 创建一个D对象
doc = Document()
item = doc.createElement('secret')
doc.appendChild(item)

for i in range(5):
    # ip
    webAdmin = doc.createElement('webAdmin')
    item.appendChild(webAdmin)
    display_ip = doc.createElement('ip')
    webAdmin.appendChild(display_ip)
    display_ip_text = doc.createTextNode('192.168.120.205')
    display_ip.appendChild(display_ip_text)

    # port
    display_port = doc.createElement('port')
    webAdmin.appendChild(display_port)
    display_port_text = doc.createTextNode('8089')
    display_port.appendChild(display_port_text)

    # https
    display_https = doc.createElement('https')
    webAdmin.appendChild(display_https)
    display_https_text = doc.createTextNode('false')
    display_https.appendChild(display_https_text)

    # username
    sclice = doc.createElement('sclice')
    item.appendChild(sclice)
    sclice_username = doc.createElement('userName')
    sclice.appendChild(sclice_username)
    sclice_username_text = doc.createTextNode(f'ka{i+1}')
    sclice_username.appendChild(sclice_username_text)

    # pwd
    sclice_pwd = doc.createElement('pwd')
    sclice.appendChild(sclice_pwd)
    sclice_pwd_text = doc.createTextNode('c123456!')
    sclice_pwd.appendChild(sclice_pwd_text)

    # content
    sclice_content = doc.createElement('content')
    sclice.appendChild(sclice_content)
    display_content_text = doc.createTextNode('None')
    sclice_content.appendChild(display_content_text)

f = open('config.xml', 'w')
doc.writexml(f, indent='\t', newl='\n', addindent='\t', encoding='utf-8')
f.close()
print("Fine")


结果:

image-1667878797834

3

评论区