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. Linux内核 实践二

    实践二 内核模块编译 20135307 张嘉琪 一.实验原理 Linux模块是一些可以作为独立程序来编译的函数和数据类型的集合.之所以提供模块机制,是因为Linux本身是一个单内核.单内核由于所有内容 ...

  2. Python学习笔记 --第二章

    Python语法基础 "#"号为注释符,建议缩进四个空格,Python大小写敏感. 数据类型 整数 0,2等等,以0x开头的为十六进制数 浮点数 1.58e9 字符串 用'或"括起来的任意文 ...

  3. 业务-----部门Service常用逻辑

    1.org实体类 public class Org implements Serializable { private static final long serialVersionUID = 1L; ...

  4. git使用命令记录

    一,两个概念:1.工作区:你电脑里能看见的目录,比如一个项目文件夹就是一个工作区2.版本库工作区(该项目的文件夹)中有一个隐藏文件 .git ,就是git的版本库.(这个文件默认是隐藏,Ctrl+h ...

  5. FreeMaker使用HashMap

    private Map<String, Object> variables; <input type="hidden" id="tongzhisbm&q ...

  6. ECSHOP广告调用广告位添加到首页顶部通栏教程

    ECSHOP广告调用广告位添加到首页顶部通栏教程 ECSHOP教程/ ecshop教程网(www.ecshop119.com) 2012-05-26   ECSHOP系统默认预留的广告位很少,如何才能 ...

  7. Node 表单query

    //#使用nodejs编写动态的web服务器//1:加载需要模块 fs http urlconst fs = require("fs");const http = require( ...

  8. aliyun centos14.04 trusty 上安装docker1.12.1

    现在apt这边拿到的docker最新版本就是1.12.1 其实本来这次不准备记录了,本以为一帆风顺的安装最后还是遇到了一点坑,aliyun的锅,卡成狗无法下载.青岛机房 1.更新源,然后安装ca-ce ...

  9. 自动化运维python学习笔记一

    Python简介 python是吉多·范罗苏姆发明的一种面向对象的脚本语言,可能有些人不知道面向对象和脚本具体是什么意思,但是对于一个初学者来说,现在并不需要明白.大家都知道,当下全栈工程师的概念很火 ...

  10. 浅谈cpu.idle和cpu.load

    1.概述 大家经常对一个系统的容量进行评估时,会参考cpu.idle和cpu.load指标,但是这两个指标到底在什么区间,表示系统是正常或者异常呢,业内有不同的说法.因此本文搜集一些资料,并对一个系统 ...