读取只包含标签的xml
什么是XML
XML是可扩展标记语言(Extensible Markup Language)的缩写,其中标记是关键部分。用户可以创建内容,然后使用限定标记标记它,从而使每个单词、短语或块成为可识别、可分类的信息。
标记语言从早起的私有公司和政府制定形式逐渐演变成标准通用标记语言(Standard Generalized Markup Language,SGML)、超文本标记语言(Hypertext Markup Language,HTML),并且最终演变成XML。XML有以下几个特点:
- XML的设计宗旨是传输数据,而非显示数据
- XML的标签没有被预定义,用户需要自行定义标签
- XML被设计为具有自我描述性
- XML是W3C的推荐标准
Python对XML文件的解析
常见的XML编程接口有DOM和SAX,这两种接口处理XML文件的方式不同,使用场合也不同。DOM是由W3C官方提出的标准,它会把整个XML文件读入内存,并将该文件解析成树,我们可以通过访问树的节点的方式访问XML中的标签,但是这种方法占用内存大,解析慢,如果读入文件过大,尽量避免使用这种方法。SAX是事件驱动的,通过在解析XML的过程中触发一个个的事件并调用用户自定义的回调函数来处理XML文件,速度比较快,占用内存少,但是需要用户实现回调函数,因此Python标准库的官方文档中这样介绍SAX:SAX每次只允许你查看文档的一小部分,你无法通过当前获取的元素访问其他元素。Python中提供了很多包支持XML文件的解析,如xml.dom,xml.sax,xml.dom.minidom和xml.etree.ElementTree等,本文重点介绍xml.dom.minidom。
xml.dom.minidom包
xml.dom.minidom是DOM API的极简化实现,比完整版的DOM要简单的多,而且这个包也小得多,下面以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>
然后我们调用xml.dom.minidom.parse方法读入xml文件并解析成DOM树
from xml.dom.minidom import parse
import xml.dom.minidom # 使用minidom解析器打开 XML 文档
DOMTree = xml.dom.minidom.parse("F:/project/Breast/codes/AllXML/aa.xml")
collection = DOMTree.documentElement
if collection.hasAttribute("shelf"):
print("Root element : %s" % collection.getAttribute("shelf")) # 在集合中获取所有电影
movies = collection.getElementsByTagName("movie") # 打印每部电影的详细信息
for movie in movies:
print("*****Movie*****")
if movie.hasAttribute("title"):
print("Title: %s" % movie.getAttribute("title")) type = movie.getElementsByTagName('type')[0]
print("Type: %s" % type.childNodes[0].data)
format = movie.getElementsByTagName('format')[0]
print("Format: %s" % format.childNodes[0].data)
rating = movie.getElementsByTagName('rating')[0]
print("Rating: %s" % rating.childNodes[0].data)
description = movie.getElementsByTagName('description')[0]
print("Description: %s" % description.childNodes[0].data)
以上程序执行结果如下:
Root element : New Arrivals
*****Movie*****
Title: Enemy Behind
Type: War, Thriller
Format: DVD
Rating: PG
Description: Talk about a US-Japan war
*****Movie*****
Title: Transformers
Type: Anime, Science Fiction
Format: DVD
Rating: R
Description: A schientific fiction
*****Movie*****
Title: Trigun
Type: Anime, Action
Format: DVD
Rating: PG
Description: Vash the Stampede!
*****Movie*****
Title: Ishtar
Type: Comedy
Format: VHS
Rating: PG
Description: Viewable boredom
实战—批量修改XML文件
最近在用caffe-ssd训练比赛的数据集,但是官方给的数据集用来标记的XML文件并不是标准格式,一些标签的命名不对,导致无法正确生成lmdb文件,因此需要修改这些标签,下面用Python实现了一个批量修改XML文件的脚本。
# -*- coding:utf-8 -*- import os import xml.dom.minidom xml_file_path = "/home/lyz/data/VOCdevkit/MyDataSet/Annotations/"
lst_label = ["height", "width", "depth"]
lst_dir = os.listdir(xml_file_path) for file_name in lst_dir:
file_path = xml_file_path + file_name
tree = xml.dom.minidom.parse(file_path)
root = tree.documentElement #获取根结点
size_node = root.getElementsByTagName("size")[0]
for size_label in lst_label: #替换size标签下的子节点
child_tag = "img_" + size_label
child_node = size_node.getElementsByTagName(child_tag)[0]
new_node = tree.createElement(size_label)
text = tree.createTextNode(child_node.firstChild.data)
new_node.appendChild(text)
size_node.replaceChild(new_node, child_node) #替换object下的boundingbox节点
lst_obj = root.getElementsByTagName("object")
data = {}
for obj_node in lst_obj:
box_node = obj_node.getElementsByTagName("bounding_box")[0]
new_box_node = tree.createElement("bndbox")
for child_node in box_node.childNodes:
tmp_node = child_node.cloneNode("deep")
new_box_node.appendChild(tmp_node)
x_node = new_box_node.getElementsByTagName("x_left_top")[0]
xmin = x_node.firstChild.data
data["xmin"] = (xmin, x_node)
y_node = new_box_node.getElementsByTagName("y_left_top")[0]
ymin = y_node.firstChild.data
data["ymin"] = (ymin, y_node)
w_node = new_box_node.getElementsByTagName("width")[0]
xmax = str(int(xmin) + int(w_node.firstChild.data))
data["xmax"] = (xmax, w_node)
h_node = new_box_node.getElementsByTagName("height")[0]
ymax = str(int(ymin) + int(h_node.firstChild.data))
data["ymax"] = (ymax, h_node) for k, v in data.items():
new_node = tree.createElement(k)
text = tree.createTextNode(v[0])
new_node.appendChild(text)
new_box_node.replaceChild(new_node, v[1])
obj_node.replaceChild(new_box_node, box_node) with open(file_path, 'w') as f:
tree.writexml(f, indent="\n", addindent="\t", encoding='utf-8') #去掉XML文件头(一些情况下文件头的存在可能导致错误)
lines = []
with open(file_path, 'rb') as f:
lines = f.readlines()[1:]
with open(file_path, 'wb') as f:
f.writelines(lines) print("-----------------done--------------------")
关于writexml方法:indent参数表示在当前节点之前插入的字符,addindent表示在该结点的子节点前插入的字符
读取只包含标签的xml的更多相关文章
- 读取配置文件包含properties和xml文件
读取properties配置文件 /** * 读取配置文件 * @author ll-t150 */ public class Utils { private static Properties pr ...
- 死磕Spring之IoC篇 - 解析自定义标签(XML 文件)
该系列文章是本人在学习 Spring 的过程中总结下来的,里面涉及到相关源码,可能对读者不太友好,请结合我的源码注释 Spring 源码分析 GitHub 地址 进行阅读 Spring 版本:5.1. ...
- 19 标签:xml或者html
1 标签:xml或者html 1.1 使用XmlSlurper解析xml groovy处理xml非常容易.XmlSlurper 类用来处理xml.在处理xml方面,还有其他的处理方式,但 ...
- 51Nod 1010 只包含因子2 3 5的数 Label:None
K的因子中只包含2 3 5.满足条件的前10个数是:2,3,4,5,6,8,9,10,12,15. 所有这样的K组成了一个序列S,现在给出一个数n,求S中 >= 给定数的最小的数. 例如:n = ...
- 51Nod--1010 只包含235的数
51Nod: http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1010 1010 只包含因子2 3 5的数 基准时间限制:1 ...
- 做参数可以读取参数 保存参数 用xml文件的方式
做参数可以读取参数 保存参数 用xml文件的方式 好处:供不同用户保存适合自己使用的参数
- sql语句读取所有父子标签
select A.HOSPITAL_ID from T_HOSPITAL A connect by prior A.HOSPITAL_ID=A.PARENT_ID start with A.HOSPI ...
- 只包含schema的dll生成和引用方法
工作中,所有的tools里有一个project是只包含若干个schema的工程,研究了一下,发现创建这种只包含schema的dll其实非常简单. 首先,在visual studio-new proje ...
- 1007 正整数分组 1010 只包含因子2 3 5的数 1014 X^2 Mod P 1024 矩阵中不重复的元素 1031 骨牌覆盖
1007 正整数分组 将一堆正整数分为2组,要求2组的和相差最小. 例如:1 2 3 4 5,将1 2 4分为1组,3 5分为1组,两组和相差1,是所有方案中相差最少的. Input 第1行:一个 ...
随机推荐
- A Neural Influence Diffusion Model for Social Recommendation 笔记
目录 一.摘言 二.杂记 三.问题定义和一些准备工作 四.模型真思想 五.实验部分 六.参考文献 一.摘言 之前协同过滤利用user-item交互历史很好的表示了user和item.但是由于用户行为的 ...
- 166. 数独 dancing links 方法
dfs硬怼通过数独 N皇后的代码后 想学习下新的数据结构和算法来解决这类覆盖问题 习题练习 https://www.acwing.com/problem/content/168/ 数独 https:/ ...
- MYSQL ERROR:1130 解决
MYSQL ERROR:1130 解决 ERROR 1130: Host '127.0.0.7' is not allowed to connect to this MySQL server 解决 ...
- DevExpress启动时的全屏SplashScreen
使用DevExpress启动时弹出"正在加载"的Logo,而且是全屏,这种感觉不太好. 原因是使用了DocmentManager控件,当DocmentManager初始化如果耗时较 ...
- 使用ArcPy拓扑检查的基本步骤
拓扑检查是GIS的特性,在ArcGIS可使用多种方法进行检查,包括: 1.在数据集上右键按向导建立: 2.使用拓扑工具箱的一系列工具分步建立: 3.创建模型工具,制作专门的拓扑工具: 4.利用ArcP ...
- autojump--懒人利器
只有打开过的目录 autojump 才会记录,所以使用时间越长,autojump 才会越智能. 可以使用 autojump 命令,或者使用短命令 j. 跳转到指定目录 j directoryName ...
- js中的NaN,isNaN与Number.isNaN的区别,如何判断一个值严格等于NaN
在JavaScript的数字类型Number中,我们最常使用的大概是整数类型与浮点数类型,但除这两者外,还有个特殊的存在NaN,为什么NaN!==NaN?我们如何判断一个值是否等于NaN呢?这篇文章好 ...
- 打印对象(__str__()和__repr__())
当打印一个类的实例时,返回的字符串是对象的地址信息,如<__main__.Student object at 0x109afb310>,很不好看 可通过在类内定义__str__(),这样打 ...
- 《细说PHP》第四版 样章 第23章 自定义PHP接口规范 6
23.4 API的设计原则和规范 API是服务提供方和使用方之间对接的通道,前面我们设计的一些简单API的例子,基本上比较随意,没有使用任何规范.设想一下,每个平台都可能存在大量的API,如果API ...
- Flink1.7.2安装部署的几种方式
原文链接:https://blog.csdn.net/a_drjiaoda/article/details/88037282 前言:Flink的运行一般分为三种模式,即local.Standalone ...