Python xml.dom.minidom reads xml

I wrote sax before to read xml. You can click to view the details.

Try dom to read xml here

Dom reads and writes several attributes of xml

parse parses xml

documentElement gets the root node

nodeName Get the node name

nodeType get byte node

childNodes get child nodes

hasAttribute Get node attributes

getElementsByTagName This is generally to get the content according to the node name

Specifically implement demo1

<?xml version="1.0" encoding="UTF-8"?>
<config_content>
    <lib name="a" path="a的路径"/>
    <lib name="b" path="b的路径"/>
    <lib name="c" path="c的路径"/>
</config_content>

specific code

import xml.dom.minidom


def read_xml():
    # 解析xml文件
    dom = xml.dom.minidom.parse("v3_config.xml")
    # 获得根节点
    domData = dom.documentElement
    # 根据节点名称寻找节点,返回列表
    configDatas = domData.getElementsByTagName("lib")
    for i in configDatas:
        print("name的内容:%s,path的内容:%s" % (i.getAttribute("name"), i.getAttribute("path")))


if __name__ == "__main__":
    read_xml()

print result

The content of name: a, the content of path: the path of a The
content of name: b, the content of path: the path of b
The content of name: c, the content of path: the path of c

Specific demo2

<?xml version="1.0" encoding="UTF-8"?>
<config_content>
    <type class="3年级">
        <lib name="体育">优秀</lib>
        <lib name="语文">一般</lib>
        <lib name="数学">优秀</lib>
    </type>
    <type class="5年级">
        <lib name="体育">一般</lib>
        <lib name="语文">优秀</lib>
        <lib name="数学">良好</lib>
    </type>
</config_content>

specific code

import xml.dom.minidom


def read_xml():
    # 解析xml文件
    dom = xml.dom.minidom.parse("v2_config.xml")
    # 获得根节点
    domData = dom.documentElement
    # 根据节点名称寻找节点,返回列表
    configDatas = domData.getElementsByTagName("type")
    for config in configDatas:
        if config.hasAttribute("class"):
            print("======type: %s======" % config.getAttribute("class"))

        libdatas = domData.getElementsByTagName("lib")
        for i in libdatas:
            print("lib的属性: %s" % i.getAttribute("name"))
            print("lib的内容: %s" % i.childNodes[0].data)


if __name__ == "__main__":
    read_xml()

print result

======type: 3rd grade======
Lib properties: Sports
lib content: Excellent
lib properties: Chinese
lib content: General lib
properties: Mathematics
lib content: Excellent
lib properties: Mathematics
lib content: general
lib properties: language
lib content: excellent
lib properties: sports
lib content: good
======type: 5th grade ======
lib properties: sports
lib content :
Properties of excellent lib:
Contents of Chinese lib:
Properties of general lib:
Contents of math lib: Properties of excellent
lib: Contents of math
lib: Properties of general
lib: Contents of Chinese
lib: Properties of excellent
lib:
Contents of sports lib : good

Process finished with exit code 0
 

Specific demo3

<?xml version="1.0"?>
<data>
    <config name="上海">
        <access>1</access>
        <year>2008</year>
        <number>668669</number>
        <areaConfig areaId="0" minInt32Value="-1" maxInt32Value="14"/>
    </config>
    <config name="苏州">
        <access>4</access>
        <year>2022</year>
        <number>668666</number>
        <areaConfig areaId="1" minInt32Value="-10" maxInt32Value="10"/>
    </config>
    <config name="南京">
        <access>8</access>
        <year>2023</year>
        <number>668866</number>
        <areaConfig areaId="2" minInt32Value="-100" maxInt32Value="140"/>
    </config>
</data>

the code

import xml.dom.minidom


def read_xml():
    # 解析xml文件
    dom = xml.dom.minidom.parse("v1_config.xml")
    # 获得根节点
    domData = dom.documentElement
    # 根据节点名称寻找节点,返回列表
    configDatas = domData.getElementsByTagName("config")

    for config in configDatas:
        if config.hasAttribute("name"):
            # 获取最外层节点
            print("======name: %s======" % config.getAttribute("name"))
        access = config.getElementsByTagName("access")[0]
        # 获取字节点的内容
        print("access: %s" % access.childNodes[0].data)
        year = config.getElementsByTagName("year")[0]
        print("year: %s" % year.childNodes[0].data)
        number = config.getElementsByTagName("number")[0]
        print("number: %s" % number.childNodes[0].data)
        areaConfig = config.getElementsByTagName("areaConfig")
        for i in areaConfig:
            print("areaConfig的内容弄 ,areaId:%s,minInt32Value:%s,maxInt32Value:%s" % (
                i.getAttribute("areaId"), i.getAttribute("minInt32Value"), i.getAttribute("maxInt32Value")))


if __name__ == "__main__":
    read_xml()

print result

======name: Shanghai======
access: 1
year: 2008
number: 668669
areaConfig content lane, areaId: 0, minInt32Value: -1, maxInt32Value: 14
======name: Suzhou ======
access: 4
year: 2022
number: 668666
The content of areaConfig, areaId: 1, minInt32Value: -10, maxInt32Value: 10
======name: Nanjing======
access: 8
year: 2023
number: 668866
The content of areaConfig, areaId: 2, minInt32Value: -100, maxInt32Value: 140

Process finished with exit code 0
 

Guess you like

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