参考文章: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\tAttrib: %s\t\tValue: %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()

运行结果:

用到的知识点:

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必须已经存在,运行第六步,子节点的标签必须存在,运行第七步,此节点必须是文本节点,否则都会出现异常。

Python基础之读写xml总结的更多相关文章

  1. python 基础-文件读写'r' 和 'rb'区别

    原文链接: python基础-文件读写'r' 和 'rb'区别 一.Python文件读写的几种模式: r,rb,w,wb 那么在读写文件时,有无b标识的的主要区别在哪里呢? 1.文件使用方式标识 'r ...

  2. python利用lxml读写xml格式文件

    之前在转换数据集格式的时候需要将json转换到xml文件,用lxml包进行操作非常方便. 1. 写xml文件 a) 用etree和objectify from lxml import etree, o ...

  3. python基础-文件读写'r' 和 'rb'区别

    一.Python文件读写的几种模式: r,rb,w,wb 那么在读写文件时,有无b标识的的主要区别在哪里呢? 1.文件使用方式标识 'r':默认值,表示从文件读取数据.'w':表示要向文件写入数据,并 ...

  4. python基础之读取xml

    python怎么操作xml文件详细介绍链接:https://www.jb51.net/article/50812.htm 从结构上来说,xml很像常见的HTML超文本标记语言.不过超文本语言被设计用来 ...

  5. python基础 - 文件读写

    完成功能: 从指定位置读文件到控制台 #! /usr/bin/python # coding=utf- 方法一. try: f = open ('/root/python/file/001.txt', ...

  6. python 基础 ---- 文件读写

    文件是一种存储在存储存储媒介上的信息或数据 常用的文件类型 文件 的打开关闭 close() 关闭文件 文件的打开路径 绝对路径 : 文件在操作系统中标准的存放路径 相对路径: 与目前引用文件的相对位 ...

  7. python基础之文件读写

    python基础之文件读写 本节内容 os模块中文件以及目录的一些方法 文件的操作 目录的操作 1.os模块中文件以及目录的一些方法 python操作文件以及目录可以使用os模块的一些方法如下: 得到 ...

  8. 第二篇:python基础之文件读写

    python基础之文件读写   python基础之文件读写 本节内容 os模块中文件以及目录的一些方法 文件的操作 目录的操作 1.os模块中文件以及目录的一些方法 python操作文件以及目录可以使 ...

  9. 七. Python基础(7)--文件的读写

    七. Python基础(7)--文件的读写 1 ● 文件读取的知识补充 f = open('file', encoding = 'utf-8') content1 = f.read() content ...

随机推荐

  1. 【NX二次开发】获取片体的边UF_MODL_ask_body_boundaries()

    获取片体的边UF_MODL_ask_body_boundaries(),如果输入实体,则获取的片体的边的数量为0. 源码: #include "Text.h" extern Dll ...

  2. 大白话带你认识JVM

    如果在文中用词或者理解方面出现问题,欢迎指出.此文旨在提及和而不深究,但会尽量效率地把知识点都抛出来 一.JVM的基本介绍 JVM 是 Java Virtual Machine 的缩写,它是一个虚构出 ...

  3. golang 写文件--详细解释

    1,不覆盖指定的文件 先看代码怎么写,下面再具体解释. func writeToFile(msg string) { f, err := os.OpenFile("/home/mingbai ...

  4. Go语言获取Ubuntu所有网卡名

    Go语言获取Ubuntu所有网卡名 需求 获取当前机器下所有网卡名,以字符串数组的形式返回 实现demo package main import ( "fmt" "os/ ...

  5. 使⽤Swagger2构建强⼤的RESTful API⽂档

    使⽤Swagger2构建强⼤的RESTful API⽂档 导语: 由于Spring Boot能够快速开发.便捷部署等特性,相信有很⼤⼀部分Spring Boot的⽤户会⽤来构建RESTful API. ...

  6. MySQL—Dos命令操作数据库

    MySQL 是一个关系型数据库,存在表的概念 结构,数据库可以存放多张表,每个表可以存放多个字段,每个字段可以存放多个记录 Dos命令操作数据库 PhpStudy使用终端打开数据库 第一次打开默认的密 ...

  7. SonarQube集成Gitlab

    SonarQube配置gitlab Gitlab创建访问Token 拷贝此Token至Sonarqube进行配置 检查配置配置成功 可以看到权限内的项目

  8. 9、SpringBoot整合之SpringBoot整合SpringSecurity

    SpringBoot整合SpringSecurity 一.创建项目,选择依赖 选择Spring Web.Thymeleaf即可 二.在pom文件中导入相关依赖 <!-- 导入SpringSecu ...

  9. redis-list实现

    Redis 数据结构---链表 Redis的list底层实现使用的不是数组而是链表的数据结构 叫listnode  是一个双向链表 ListNode{ Struct listNode *prev  / ...

  10. Redis:redis.conf配置文件 - 及配置详解

    配置文件详解(文章最后有完整的redis.conf文件) ###################################  NETWORK  ######################### ...