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\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总结的更多相关文章
- python 基础-文件读写'r' 和 'rb'区别
原文链接: python基础-文件读写'r' 和 'rb'区别 一.Python文件读写的几种模式: r,rb,w,wb 那么在读写文件时,有无b标识的的主要区别在哪里呢? 1.文件使用方式标识 'r ...
- python利用lxml读写xml格式文件
之前在转换数据集格式的时候需要将json转换到xml文件,用lxml包进行操作非常方便. 1. 写xml文件 a) 用etree和objectify from lxml import etree, o ...
- python基础-文件读写'r' 和 'rb'区别
一.Python文件读写的几种模式: r,rb,w,wb 那么在读写文件时,有无b标识的的主要区别在哪里呢? 1.文件使用方式标识 'r':默认值,表示从文件读取数据.'w':表示要向文件写入数据,并 ...
- python基础之读取xml
python怎么操作xml文件详细介绍链接:https://www.jb51.net/article/50812.htm 从结构上来说,xml很像常见的HTML超文本标记语言.不过超文本语言被设计用来 ...
- python基础 - 文件读写
完成功能: 从指定位置读文件到控制台 #! /usr/bin/python # coding=utf- 方法一. try: f = open ('/root/python/file/001.txt', ...
- python 基础 ---- 文件读写
文件是一种存储在存储存储媒介上的信息或数据 常用的文件类型 文件 的打开关闭 close() 关闭文件 文件的打开路径 绝对路径 : 文件在操作系统中标准的存放路径 相对路径: 与目前引用文件的相对位 ...
- python基础之文件读写
python基础之文件读写 本节内容 os模块中文件以及目录的一些方法 文件的操作 目录的操作 1.os模块中文件以及目录的一些方法 python操作文件以及目录可以使用os模块的一些方法如下: 得到 ...
- 第二篇:python基础之文件读写
python基础之文件读写 python基础之文件读写 本节内容 os模块中文件以及目录的一些方法 文件的操作 目录的操作 1.os模块中文件以及目录的一些方法 python操作文件以及目录可以使 ...
- 七. Python基础(7)--文件的读写
七. Python基础(7)--文件的读写 1 ● 文件读取的知识补充 f = open('file', encoding = 'utf-8') content1 = f.read() content ...
随机推荐
- 『无为则无心』Python基础 — 2、编译型语言和解释型语言的区别
目录 1.什么是计算机语言 2.高级语言中的编译型语言和解释型语言 (1)编译型语言 (2)解释型语言 (3)编译型语言和解释型语言执行流程 3.知识扩展: 4.关于Python 1.什么是计算机语言 ...
- .Net Core with 微服务 - Elastic APM
上一次我们介绍了Seq日志聚合组件.这次要给大家介绍的是Elastic APM ,一款应用程序性能监控组件.APM 监控围绕对应用.服务.容器的健康监控,对接口的调用链.性能进行监控.在我们实施微服务 ...
- Local dimming algorithm in matlab
LED局部背光算法的matlab仿真 最近公司接了华星光电(TCL)的一个项目LCD-BackLight-Local-Diming-Algorithm-IP ,由于没有实际的硬件,只能根据客户给的论文 ...
- 飞扬起舞,基于.Net Cli亲手打造.Net Core团队的项目脚手架
什么是脚手架? Scaffolding is a meta-programming method of building database-backed software applications. ...
- keycloak~自定义rest接口
rest资源 对于我们集成keycloak来说,你可能会遇到它没有实现的功能,这时需要对kc进行扩展,资源的扩展是其中一个方面,它需要实现RealmResourceProvider和RealmReso ...
- elk 日志收集 filebeat 集群搭建 php业务服务日志 nginx日志 json 7.12版本 ELK 解决方案
难的不是技术,难的是业务.熟悉业务流程才是最难的. 其实搜索进来的每一个人的需求不一样,希望你能从我的这篇文章里面收获到. 建议还是看官方文档,更全面一些. 一.背景 1,收集nginx acces ...
- 面试题二:JVM
JVM垃圾回收的时候如何确定垃圾? 有2种方式: 引用计数 每个对象都有一个引用计数属性,新增一个引用时计数加1,引用释放时计数减1,计数为0时可以回收: 缺点:无法解决对象循环引用的问题: 可达性分 ...
- java:替换List集合中的某个任意值(对象)
定义replaceAll方法,将传入的新值替换集合中的老值(list,old,new) private static <E> void replaceAll(List<E> l ...
- mysql 深度解析auto-increment自增列"Duliplicate key"问题
转载自:https://cloud.tencent.com/developer/article/1367681 问题描述 近期,线上有个重要Mysql客户的表在从5.6升级到5.7后master上插入 ...
- F5负载均衡-配置指导手册(含IPv6)
F5负载均衡-配置手册 设备概况 图形化界面 通过网络形式访问F5任一接口地址,打开浏览器输入https://网络接口地址:或pc机直连F5的MGMT带外管理口,打开浏览器,输入https://192 ...