代码
# 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")
结果: