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模块(二十四)的更多相关文章

  1. 第三百二十四节,web爬虫,scrapy模块介绍与使用

    第三百二十四节,web爬虫,scrapy模块介绍与使用 Scrapy是一个为了爬取网站数据,提取结构性数据而编写的应用框架. 其可以应用在数据挖掘,信息处理或存储历史数据等一系列的程序中.其最初是为了 ...

  2. WCF技术剖析之二十四: ServiceDebugBehavior服务行为是如何实现异常的传播的?

    原文:WCF技术剖析之二十四: ServiceDebugBehavior服务行为是如何实现异常的传播的? 服务端只有抛出FaultException异常才能被正常地序列化成Fault消息,并实现向客户 ...

  3. (C/C++学习笔记) 二十四. 知识补充

    二十四. 知识补充 ● 子类调用父类构造函数 ※ 为什么子类要调用父类的构造函数? 因为子类继承父类,会继承到父类中的数据,所以子类在进行对象初始化时,先调用父类的构造函数,这就是子类的实例化过程. ...

  4. python3.4学习笔记(二十四) Python pycharm window安装redis MySQL-python相关方法

    python3.4学习笔记(二十四) Python pycharm window安装redis MySQL-python相关方法window安装redis,下载Redis的压缩包https://git ...

  5. Bootstrap<基础二十四> 缩略图

    Bootstrap 缩略图.大多数站点都需要在网格中布局图像.视频.文本等.Bootstrap 通过缩略图为此提供了一种简便的方式.使用 Bootstrap 创建缩略图的步骤如下: 在图像周围添加带有 ...

  6. 二十四、Struts2中的UI标签

    二十四.Struts2中的UI标签 Struts2中UI标签的优势: 数据回显 页面布局和排版(Freemark),struts2提供了一些常用的排版(主题:xhtml默认 simple ajax) ...

  7. VMware vSphere 服务器虚拟化之二十四 桌面虚拟化之手动池管理物理机

    VMware vSphere 服务器虚拟化之二十四 桌面虚拟化之手动池管理物理机 VMwareView手动池可以管理物理计算机 说明: 环境基于实验二十三 1.准备一台Windows 7的物理计算机名 ...

  8. Bootstrap入门(二十四)data属性

    Bootstrap入门(二十四)data属性 你可以仅仅通过 data 属性 API 就能使用所有的 Bootstrap 插件,无需写一行 JavaScript 代码.这是 Bootstrap 中的一 ...

  9. 3360: [Usaco2004 Jan]算二十四

    3360: [Usaco2004 Jan]算二十四 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 6  Solved: 6[Submit][Statu ...

  10. JAVA之旅(二十四)——I/O流,字符流,FileWriter,IOException,文件续写,FileReader,小练习

    JAVA之旅(二十四)--I/O流,字符流,FileWriter,IOException,文件续写,FileReader,小练习 JAVA之旅林林总总也是写了二十多篇了,我们今天终于是接触到了I/O了 ...

随机推荐

  1. 【亲测有效】Kali Linux无法安装网易云音乐的解决方案

    问题描述 由于 Kali Linux 的内核是基于 Debian 的,我们在安装网易云音乐的时候更偏向于选择安装网易云音乐 v1.1.0 deepin15(64位) 的包,可是我发现在安装过程中,无法 ...

  2. git push上传代码到gitlab上,报错401/403(或需要输入用户名和密码)

    之前部署的gitlab,采用ssh方式连接gitlab,在客户机上产生公钥上传到gitlab的SSH-Keys里,git clone下载和git push上传都没问题,这种方式很安全. 后来应开发同事 ...

  3. Reading Task 2 —— by12061154Joy

    关于Silver Bullet: Brooks在“No Silver Bullet”主张并断言在未来的十年之内(从1986年文章发表后开始计算),不会有任何单一的软件工程上的突破,能够让程序设计的生产 ...

  4. 战神答题APP 无敌结束版

    APP发布了哦~~     多多捧场~ http://anzhuoyuan.com/app/info/appid/242381.html 还有github https://github.com/784 ...

  5. @ModelAttribute注解(SpringMVC)

    在方法定义上使用 @ModelAttribute 注解:Spring MVC 在调用目标处理方法前,会先逐个调用在方法级上标注了 @ModelAttribute 的方法. 在方法的入参前使用 @Mod ...

  6. opencv学习笔记(二)

    摘要:学习资料主要参考于毛星云主编<opencv3编程入门> 1.图像显示 #include<opencv2/opencv.hpp> using namespace cv; / ...

  7. docker 下运行 postgresql 的命令

    postgresql docker下启动的命令 docker run --name pgdata -p : -e POSTGRES_PASSWORD=Test6530 -v /pgdata:/var/ ...

  8. [转载]linux段页式内存管理技术

    原始博客地址: http://blog.csdn.net/qq_26626709/article/details/52742470 一.概述 1.虚拟地址空间 内存是通过指针寻址的,因而CPU的字长决 ...

  9. OneZero——Review会议(2013.5.20)

    1. 时间: 2016年5月20日. 2. 成员: X 夏一鸣 * 组长 (博客:http://www.cnblogs.com/xiaym896/), G 郭又铭 (博客:http://www.cnb ...

  10. Bootstrap按钮式下拉菜单

    前面的话 按钮式下拉菜单仅从外观上看,和下拉菜单效果基本上是一样的.不同的是普通的下拉菜单是block元素,而按钮式下拉菜单是inline-block元素.本文将详细介绍Bootstrap按钮式下拉菜 ...