XML模块(二十四)
xml是实现不同语言或程序之间进行数据交换的协议,跟json差不多,但json使用起来更简单,不过,古时候,在json还没诞生的黑暗年代,
大家只能选择用xml呀,至今很多传统公司如金融行业的很多系统的接口还主要是xml。
xml的格式如下,就是通过<>节点来区别数据结构的:
<?xml version="1.0"?>
<data>
<country name="Liechtenstein">
<rank updated="yes">2</rank>
<year>2008</year>
<gdppc>141100</gdppc>
<neighbor name="Austria" direction="E"/>
<neighbor name="Switzerland" direction="W"/>
</country>
<country name="Singapore">
<rank updated="yes">5</rank>
<year>2011</year>
<gdppc>59900</gdppc>
<neighbor name="Malaysia" direction="N"/>
</country>
<country name="Panama">
<rank updated="yes">69</rank>
<year>2011</year>
<gdppc>13600</gdppc>
<neighbor name="Costa Rica" direction="W"/>
<neighbor name="Colombia" direction="E"/>
</country>
</data>
xml协议在各个语言里的都 是支持的,在python中可以用以下模块操作xml:
# print(root.iter('year')) #全文搜索
# print(root.find('country')) #在root的子节点找,只找一个
# print(root.findall('country')) #在root的子节点找,找所有
查:
import xml.etree.ElementTree as ET
tree = ET.parse('xml_l')
root = tree.getroot()
# 只拿year节点
for year in root.iter('year'):
print(year.tag,year.text)
'''
year 2008
year 2011
year 2011
'''
import xml.etree.ElementTree as ET
tree = ET.parse('xml_l')
root = tree.getroot()
for i in root:
print(i)
print(i.tag) # tag 标签名
print(i.attrib) # 属性{'name': 'Liechtenstein'}
for j in i:
print(j.tag)
print(j.attrib) # {'updated': 'yes'}
print(j.text)
'''
<Element 'country' at 0x022D96F0>
country
{'name': 'Liechtenstein'}
rank
{'updated': 'yes'}
2
year
{}
2008
gdppc
{}
141100
neighbor
{'name': 'Austria', 'direction': 'E'}
None
neighbor
{'name': 'Switzerland', 'direction': 'W'}
None
<Element 'country' at 0x022D9840>
country
{'name': 'Singapore'}
rank
{'updated': 'yes'}
5
year
{}
2011
gdppc
{}
59900
neighbor
{'name': 'Malaysia', 'direction': 'N'}
None
<Element 'country' at 0x022D9960>
country
{'name': 'Panama'}
rank
{'updated': 'yes'}
69
year
{}
2011
gdppc
{}
13600
neighbor
{'name': 'Costa Rica', 'direction': 'W'}
None
neighbor
{'name': 'Colombia', 'direction': 'E'}
None
'''
修改:
import xml.etree.ElementTree as ET
tree = ET.parse("xml_l")
root = tree.getroot()
# 修改
for year in root.iter('year'):
new_year = int(year.text) + 1
year.text = str(new_year)
year.set('update','yes') # 增加属性
tree.write("new_xml.xml")
new_xml.xml
<data>
<country name="Liechtenstein">
<rank updated="yes">2</rank>
<year update="yes">2009</year>
<gdppc>141100</gdppc>
<neighbor direction="E" name="Austria" />
<neighbor direction="W" name="Switzerland" />
</country>
<country name="Singapore">
<rank updated="yes">5</rank>
<year update="yes">2012</year>
<gdppc>59900</gdppc>
<neighbor direction="N" name="Malaysia" />
</country>
<country name="Panama">
<rank updated="yes">69</rank>
<year update="yes">2012</year>
<gdppc>13600</gdppc>
<neighbor direction="W" name="Costa Rica" />
<neighbor direction="E" name="Colombia" />
</country>
</data>
import xml.etree.ElementTree as ET
tree = ET.parse("xml_l")
root = tree.getroot()
for country in root.findall('country'):
for year in country.findall('year'):
if int(year.text) > 2000:
year2 = ET.Element('year2')
year2.text = 'NewYear'
year2.attrib = {'update':'yes'}
country.append(year2) # 往country下添加子节点
tree.write('xml_l_swap.xml')
<data>
<country name="Liechtenstein">
<rank updated="yes">2</rank>
<year>2008</year>
<gdppc>141100</gdppc>
<neighbor direction="E" name="Austria" />
<neighbor direction="W" name="Switzerland" />
<year2 update="yes">NewYear</year2></country>
<country name="Singapore">
<rank updated="yes">5</rank>
<year>2011</year>
<gdppc>59900</gdppc>
<neighbor direction="N" name="Malaysia" />
<year2 update="yes">NewYear</year2></country>
<country name="Panama">
<rank updated="yes">69</rank>
<year>2011</year>
<gdppc>13600</gdppc>
<neighbor direction="W" name="Costa Rica" />
<neighbor direction="E" name="Colombia" />
<year2 update="yes">NewYear</year2></country>
</data>
删除:
import xml.etree.ElementTree as ET
tree = ET.parse("xml_l")
root = tree.getroot()
# 删除
for country in root.findall('country'):
rank = int(country.find('rank').text)
if rank > 50:
root.remove(country)
tree.write('new_xml2.xml')
new_xml2.xml
<data>
<country name="Liechtenstein">
<rank updated="yes">2</rank>
<year>2008</year>
<gdppc>141100</gdppc>
<neighbor direction="E" name="Austria" />
<neighbor direction="W" name="Switzerland" />
</country>
<country name="Singapore">
<rank updated="yes">5</rank>
<year>2011</year>
<gdppc>59900</gdppc>
<neighbor direction="N" name="Malaysia" />
</country>
</data>
创建XML:
import xml.etree.ElementTree as ET
my_xml = ET.Element("namelist")
name = ET.SubElement(my_xml, "name", attrib={"enrolled":"yes"})
age = ET.SubElement(name, "age", attrib={"checked":"no"})
sex = ET.SubElement(name, "sex")
sex.text = "man"
name2 = ET.SubElement(my_xml, "name1", attrib={"enrolled":"no"})
age = ET.SubElement(name2, "age")
age.text = ""
et = ET.ElementTree(my_xml) # 生成文档对象
et.write("text.xml", encoding="utf-8", xml_declaration=True)
text.xml
<?xml version='1.0' encoding='utf-8'?>
<namelist>
<name enrolled="yes">
<age checked="no" />
<sex>man</sex>
</name>
<name1 enrolled="no">
<age>18</age>
</name1>
</namelist>
XML模块(二十四)的更多相关文章
- 第三百二十四节,web爬虫,scrapy模块介绍与使用
第三百二十四节,web爬虫,scrapy模块介绍与使用 Scrapy是一个为了爬取网站数据,提取结构性数据而编写的应用框架. 其可以应用在数据挖掘,信息处理或存储历史数据等一系列的程序中.其最初是为了 ...
- WCF技术剖析之二十四: ServiceDebugBehavior服务行为是如何实现异常的传播的?
原文:WCF技术剖析之二十四: ServiceDebugBehavior服务行为是如何实现异常的传播的? 服务端只有抛出FaultException异常才能被正常地序列化成Fault消息,并实现向客户 ...
- (C/C++学习笔记) 二十四. 知识补充
二十四. 知识补充 ● 子类调用父类构造函数 ※ 为什么子类要调用父类的构造函数? 因为子类继承父类,会继承到父类中的数据,所以子类在进行对象初始化时,先调用父类的构造函数,这就是子类的实例化过程. ...
- python3.4学习笔记(二十四) Python pycharm window安装redis MySQL-python相关方法
python3.4学习笔记(二十四) Python pycharm window安装redis MySQL-python相关方法window安装redis,下载Redis的压缩包https://git ...
- Bootstrap<基础二十四> 缩略图
Bootstrap 缩略图.大多数站点都需要在网格中布局图像.视频.文本等.Bootstrap 通过缩略图为此提供了一种简便的方式.使用 Bootstrap 创建缩略图的步骤如下: 在图像周围添加带有 ...
- 二十四、Struts2中的UI标签
二十四.Struts2中的UI标签 Struts2中UI标签的优势: 数据回显 页面布局和排版(Freemark),struts2提供了一些常用的排版(主题:xhtml默认 simple ajax) ...
- VMware vSphere 服务器虚拟化之二十四 桌面虚拟化之手动池管理物理机
VMware vSphere 服务器虚拟化之二十四 桌面虚拟化之手动池管理物理机 VMwareView手动池可以管理物理计算机 说明: 环境基于实验二十三 1.准备一台Windows 7的物理计算机名 ...
- Bootstrap入门(二十四)data属性
Bootstrap入门(二十四)data属性 你可以仅仅通过 data 属性 API 就能使用所有的 Bootstrap 插件,无需写一行 JavaScript 代码.这是 Bootstrap 中的一 ...
- 3360: [Usaco2004 Jan]算二十四
3360: [Usaco2004 Jan]算二十四 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 6 Solved: 6[Submit][Statu ...
- JAVA之旅(二十四)——I/O流,字符流,FileWriter,IOException,文件续写,FileReader,小练习
JAVA之旅(二十四)--I/O流,字符流,FileWriter,IOException,文件续写,FileReader,小练习 JAVA之旅林林总总也是写了二十多篇了,我们今天终于是接触到了I/O了 ...
随机推荐
- 老牌阅读器nook2刷机整理
kindle肯定是现在大多数人了解电纸书这个产品的开端,也给我留下了一段美好的回忆,不折腾,不死机,官方书城让人省心不少,不过作为半个折腾爱好者,kindle显然不符合我的理念,遂慢慢入了安卓电纸书的 ...
- npm脚本探析
什么是 npm 脚本? 在package.json文件里面,使用scripts字段定义的脚本命令 { // ... "scripts": { "build": ...
- Indidual Homework Assignment
一.Pair work的得与失 合作编程在以前的学习过程中也进行过,基本也就是各人负责一部分最后再将之拼凑起来,而这次作业要求的双人合作,要求的并不是这样,而是两人应该在一起进行工作,这样的要求理想情 ...
- Linux学习期中总结
一.<Linux内核分析>总结 (一)计算机是如何工作的 1.存储程序计算机工作模型 2. X86CPU的寄存器:通用寄存器.段寄存器.标志寄存器等. 3.计算机的汇编指令 (1)movl ...
- 阅读<构建之法>13、14、15、16、17章
13章 这么多测试为什么不能整理出一个包括所有功能的测试呢?看着那么多测试都感觉奇怪了. 14章 怎样才能体现一个测试人员的工作价值呢?这样的判断又是否会太独断了? 15章 在时间上,会不会因不同功能 ...
- HDOJ2004_成绩转换
水题:用数组标识各个阶段分数的等级即可. HDOJ2004_成绩转换 #include<stdio.h> #include<stdlib.h> #include<math ...
- vue 项目引入字体报错
vue 项目引入特殊字体,总是提示有问题 原因是,在webpack 里面的配置有问题 在项目文件里面引入字体的时候,应该写url-loader 而不能是url
- IP工具类
import org.slf4j.Logger; import org.slf4j.LoggerFactory; import javax.servlet.http.HttpServletReques ...
- PAT 1076 Wifi密码
https://pintia.cn/problem-sets/994805260223102976/problems/994805262622244864 下面是微博上流传的一张照片:“各位亲爱的同学 ...
- 在CentOS 7中搭建Git服务器
环境说明 - CentOS 7.x 最小安装 - 配置网络连接 1. 安装Git及创建用户 # 安装Git $ yum install git # 创建一个git用户组和用户,用来运行git服务 $ ...