Python xml.dom.minidom 读取xml

之前写过sax 读取xml 具体的可以点击查看

这里试下dom 读取xml

dom 读写xml的几个属性

parse 解析xml

documentElement 获取根节点

nodeName 获取节点名称

nodeType 获取字节点

childNodes 获取子节点

hasAttribute 获取节点属性

getElementsByTagName 这个一般是根据节点名称获取内容

具体实现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>

具体代码

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

打印结果

name的内容:a,path的内容:a的路径
name的内容:b,path的内容:b的路径
name的内容:c,path的内容:c的路径

具体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>

具体代码

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

打印结果

======type: 3年级======
lib的属性: 体育
lib的内容: 优秀
lib的属性: 语文
lib的内容: 一般
lib的属性: 数学
lib的内容: 优秀
lib的属性: 数学
lib的内容: 一般
lib的属性: 语文
lib的内容: 优秀
lib的属性: 体育
lib的内容: 良好
======type: 5年级======
lib的属性: 体育
lib的内容: 优秀
lib的属性: 语文
lib的内容: 一般
lib的属性: 数学
lib的内容: 优秀
lib的属性: 数学
lib的内容: 一般
lib的属性: 语文
lib的内容: 优秀
lib的属性: 体育
lib的内容: 良好

Process finished with exit code 0
 

具体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>

代码

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

打印结果

======name: 上海======
access: 1
year: 2008
number: 668669
areaConfig的内容弄 ,areaId:0,minInt32Value:-1,maxInt32Value:14
======name: 苏州======
access: 4
year: 2022
number: 668666
areaConfig的内容弄 ,areaId:1,minInt32Value:-10,maxInt32Value:10
======name: 南京======
access: 8
year: 2023
number: 668866
areaConfig的内容弄 ,areaId:2,minInt32Value:-100,maxInt32Value:140

Process finished with exit code 0
 

猜你喜欢

转载自blog.csdn.net/qq_33210042/article/details/130846945