xml.dom.minidom 里面的几个属性
1 Document() 创建dom
2 createElement() 创建标签
3 createTextNode() 创建标签内容
4 appendChild() 拼接标签或者内容
5 setAttribute(key,value) 设置标签属性
熟悉这个属性之后写几个demo熟悉
demo1
假如要写的xml 如下
<?xml version="1.0" encoding="utf-8"?>
<config>
<access>10086</access>
<access1>10087</access1>
<access2>10088</access2>
</config>
具体实现代码
import xml.dom.minidom
def write_xml():
# 创建dom对应
dom = xml.dom.minidom.Document()
# 创建xml的head
dom_head = dom.createElement("config")
# 把head放到dom中
dom.appendChild(dom_head)
# xml标签
dom_body = dom.createElement("access")
# xml内容
dom_value = dom.createTextNode('10086')
# 把内容放到标签中
dom_body.appendChild(dom_value)
# 把标签放到head中
dom_head.appendChild(dom_body)
# xml标签
dom_body = dom.createElement("access1")
# xml内容
dom_value = dom.createTextNode('10087')
# 把内容放到标签中
dom_body.appendChild(dom_value)
# 把标签放到head中
dom_head.appendChild(dom_body)
# xml标签
dom_body = dom.createElement("access2")
# xml内容
dom_value = dom.createTextNode('10088')
# 把内容放到标签中
dom_body.appendChild(dom_value)
# 把标签放到head中
dom_head.appendChild(dom_body)
with open("config.test.xml", "w", encoding="utf-8") as f:
dom.writexml(f, indent="", addindent="\t", newl="\n", encoding="utf-8")
if __name__ == "__main__":
write_xml()
demo2
假如要写的xml 如下
<?xml version="1.0" encoding="utf-8"?>
<config name="开花">
<access property="1" url="http">10086</access>
<access1 property="1" url="http">10087</access1>
<access2 property="1" url="http">10088</access2>
</config>
具体的代码如下
import xml.dom.minidom
def write_xml():
# 创建dom对应
dom = xml.dom.minidom.Document()
# 创建xml的head
dom_head = dom.createElement("config")
# 把head放到dom中
dom.appendChild(dom_head)
# 给head 标签添加属性
dom_head.setAttribute("name", "开花")
# xml标签
dom_body = dom.createElement("access")
# 给内容标签添加属性
dom_body.setAttribute("property", "1")
dom_body.setAttribute("url", "http")
# xml内容
dom_value = dom.createTextNode('10086')
# 把内容放到标签中
dom_body.appendChild(dom_value)
# 把标签放到head中
dom_head.appendChild(dom_body)
# xml标签
dom_body = dom.createElement("access1")
# xml内容
dom_value = dom.createTextNode('10087')
# 把内容放到标签中
dom_body.appendChild(dom_value)
# 给内容标签添加属性
dom_body.setAttribute("property", "1")
dom_body.setAttribute("url", "http")
# 把标签放到head中
dom_head.appendChild(dom_body)
# xml标签
dom_body = dom.createElement("access2")
# xml内容
dom_value = dom.createTextNode('10088')
# 把内容放到标签中
dom_body.appendChild(dom_value)
# 给内容标签添加属性
dom_body.setAttribute("property", "1")
dom_body.setAttribute("url", "http")
# 把标签放到head中
dom_head.appendChild(dom_body)
with open("config.test.xml", "w", encoding="utf-8") as f:
dom.writexml(f, indent="", addindent="\t", newl="\n", encoding="utf-8")
if __name__ == "__main__":
write_xml()
demo3
假如要实现的xml如下
<?xml version="1.0" encoding="utf-8"?>
<config>
<configdata index="101">
<data>
<status>1</status>
<title>三目</title>
<day>5月25日</day>
<property>0x19c2</property>
</data>
</configdata>
</config>
要实现的代码如下
import xml.dom.minidom
def write_xml():
# 创建dom对应
dom = xml.dom.minidom.Document()
# 创建xml的head
dom_config = dom.createElement("config")
# 把head放到dom中
dom.appendChild(dom_config)
dom_config_data = dom.createElement("configdata")
dom_config_data.setAttribute("index", "101")
dom_config.appendChild(dom_config_data)
dom_data = dom.createElement("data")
dom_status = dom.createElement("status")
dom_status_text = dom.createTextNode("1")
dom_status.appendChild(dom_status_text)
dom_data.appendChild(dom_status)
dom_status = dom.createElement("title")
dom_status_text = dom.createTextNode("三目")
dom_status.appendChild(dom_status_text)
dom_data.appendChild(dom_status)
dom_status = dom.createElement("day")
dom_status_text = dom.createTextNode("5月25日")
dom_status.appendChild(dom_status_text)
dom_data.appendChild(dom_status)
dom_status = dom.createElement("property")
dom_status_text = dom.createTextNode("0x19c2")
dom_status.appendChild(dom_status_text)
dom_data.appendChild(dom_status)
dom_config_data.appendChild(dom_data)
with open("config.test.xml", "w", encoding="utf-8") as f:
dom.writexml(f, indent="", addindent="\t", newl="\n", encoding="utf-8")
if __name__ == "__main__":
write_xml()