zoukankan      html  css  js  c++  java
  • Python基础之读写xml总结

    参考文章:https://blog.csdn.net/weixin_42749767/article/details/82770563

    先介绍xml.dom.minidom包,有一个读写的例子

    read_write_xml.py

    from xml.dom.minidom import parse
    import xml.dom.minidom
    import os
    
    
    def is_xml_exist(xml_path):
        xml_exist = os.path.exists(xml_path)
        if not xml_exist:
            return False
        return True
    
    
    """
    movie.xml
    <collection shelf="New Arrivals">
        <movie title="Enemy Behind">
            <type>War, Thriller</type>
            <format>DVD</format>
            <year>2003</year>
            <rating>PG</rating>
            <stars>10</stars>
            <description>Talk about a US-Japan war</description>
        </movie>
        <movie title="Transformers">
            <type>Anime, Science Fiction</type>
            <format>DVD</format>
            <year>1989</year>
            <rating>R</rating>
            <stars>8</stars>
            <description>A schientific fiction</description>
        </movie>
        <movie title="Trigun">
            <type>Anime, Action</type>
            <format>DVD</format>
            <episodes>4</episodes>
            <rating>PG</rating>
            <stars>10</stars>
            <description>Vash the Stampede!</description>
        </movie>
        <movie title="Ishtar">
            <type>Comedy</type>
            <format>VHS</format>
            <rating>PG</rating>
            <stars>2</stars>
            <description>Viewable boredom</description>
        </movie>
    </collection>
    """
    
    
    def read_movie_xml():
        path = "movie.xml"
        if not is_xml_exist(path):
            print("%s is not exist" % path)
        else:
            # 使用minidom解析器打开XML文档
            open_xml = parse(path)
            root_node = open_xml.documentElement
    
            shelf_attrib = "shelf"
            if root_node.hasAttribute(shelf_attrib):
                print("Lable: %s	Attrib: %s		Value: %s" % (
                    root_node.nodeName, shelf_attrib, root_node.getAttribute(shelf_attrib)))
            print("")
            # 在集合中获取所有电影
            movie_node = "movie"
            movies = root_node.getElementsByTagName(movie_node)
    
            # 打印每部电影的详细信息
            for movie in movies:
                print("**** Movie ****")
                if movie.hasAttribute("title"):
                    print("Title: %s" % movie.getAttribute("title"))
    
                type_movie = movie.getElementsByTagName('type')[0]
                print("Type: %s" % type_movie.childNodes[0].data)
    
                format_movie = movie.getElementsByTagName('format')[0]
                print("Format: %s" % format_movie.childNodes[0].data)
    
                rating_movie = movie.getElementsByTagName('rating')[0]
                print("Rating: %s" % rating_movie.childNodes[0].data)
    
                descrip_movie = movie.getElementsByTagName('description')[0]
                print("Rating: %s" % descrip_movie.childNodes[0].data)
    
                print("")
    
    
    if __name__ == "__main__":
        read_movie_xml()
    View Code

    运行结果:

    用到的知识点:

    1. 导入xml包:

    from xml.dom.minidom import parse

    2. 打开xml文件:

    open_xml = parse(path)
    root_node = open_xml.documentElement

    3. 获取节点名称:

    root_node.nodeName

    4. 判断节点属性是否存在:

    root_node.hasAttribute(shelf_attrib)

    5. 获取节点属性:

    root_node.getAttribute(shelf_attrib)

    6. 获取子节点对象:

    root_node.getElementsByTagName(movie_node)

    7. 获取文本节点的文本信息:

    type_movie.childNodes[0].data

    以上语句务必正确使用,运行第二步,xml必须已经存在,运行第六步,子节点的标签必须存在,运行第七步,此节点必须是文本节点,否则都会出现异常。

  • 相关阅读:
    JeeSite4.x 搭建并部署到服务器
    maven编译时出现There are test failures
    ecplise An incompatible version [1.2.14] of the APR based Apache Tomcat Native library is installed, while T
    maven "mvn不是内部或外部命令,也不是可运行的程序或批处理文件"
    rar自动压缩备份
    mysql 0x80004005 unable to connect to any of the specified mysql hosts
    mysql too many connections
    输出控制台信息到日志 并 通过cronolog对tomcat进行日志切分
    Node.js相关——package概念及NPM
    Node.js相关——CommonJS规范
  • 原文地址:https://www.cnblogs.com/smart-zihan/p/12015192.html
Copyright © 2011-2022 走看看