Python xml.dom.minidom 写xml

Several attributes in xml.dom.minidom

1 Document() creates dom

2 createElement() create label

3 createTextNode() Create label content

4 appendChild() splicing labels or content

5 setAttribute(key,value) Set label attributes

After getting familiar with this attribute, write a few demos to get familiar with

demo1

If the xml to be written is as follows

<?xml version="1.0" encoding="utf-8"?>
<config>
	<access>10086</access>
	<access1>10087</access1>
	<access2>10088</access2>
</config>

Concrete implementation code

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

If the xml to be written is as follows

<?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>

The specific code is as follows

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

If the xml to be implemented is as follows

<?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>

The code to implement is as follows

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()

Guess you like

Origin blog.csdn.net/qq_33210042/article/details/130868559