22.XML
转载:https://www.cnblogs.com/yuanchenqi/article/5732581.html
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数据
xml协议在各个语言里的都 是支持的,在python中可以用以下模块操作xml:
import xml.etree.ElementTree as ET tree = ET.parse("xmltest.xml")
root = tree.getroot()
print(root.tag) #遍历xml文档
for child in root:
print(child.tag, child.attrib)
for i in child:
print(i.tag,i.text) #只遍历year 节点
for node in root.iter('year'):
print(node.tag,node.text)
#--------------------------------------- import xml.etree.ElementTree as ET tree = ET.parse("xmltest.xml")
root = tree.getroot() #修改
for node in root.iter('year'):
new_year = int(node.text) + 1
node.text = str(new_year)
node.set("updated","yes") tree.write("xmltest.xml") #删除node
for country in root.findall('country'):
rank = int(country.find('rank').text)
if rank > 50:
root.remove(country) tree.write('output.xml')
自己创建xml文档:
import xml.etree.ElementTree as ET new_xml = ET.Element("namelist")
name = ET.SubElement(new_xml,"name",attrib={"enrolled":"yes"})
age = ET.SubElement(name,"age",attrib={"checked":"no"})
sex = ET.SubElement(name,"sex")
sex.text = ''
name2 = ET.SubElement(new_xml,"name",attrib={"enrolled":"no"})
age = ET.SubElement(name2,"age")
age.text = '' et = ET.ElementTree(new_xml) #生成文档对象
et.write("test.xml", encoding="utf-8",xml_declaration=True) ET.dump(new_xml) #打印生成的格式
DEMO
import xml.etree.ElementTree as ET
# 创建
'''
<?xml version='1.0' encoding='utf-8'?>
<peoples>
<people show="yes">
<name show="yes">ray</name>
<age show="yes">22</age>
<sex show="yes">male</sex>
</people>
</peoples>
'''
new_xml = ET.Element("peoples")
people = ET.SubElement(new_xml,"people",attrib={"show":"yes"})
name = ET.SubElement(people,"name",attrib={"show":"yes"})
age = ET.SubElement(people,"age",attrib={"show":"yes"})
sex = ET.SubElement(people,"sex",attrib={"show":"yes"})
name.text = 'ray'
age.text = ''
sex.text = 'male'
et = ET.ElementTree(new_xml)
et.write("test1.xml", encoding="utf-8", xml_declaration=True)
ET.dump(new_xml) # 操作
tree = ET.parse('test1.xml')
root = tree.getroot()
# 修改
# for node in root.iter('age'):
# new_age = int(node.text)+1
# node.text = str(new_age)
# node.set('updated','yes')
# tree.write('test1.xml')
# 删除
# for people in root.findall('people'):
# name = people.find('name').text
# if(name == 'ray'):
# root.remove(people)
# tree.write('test1.xml')
22.XML的更多相关文章
- python序列化及其相关模块(json,pickle,shelve,xml)详解
什么是序列化对象? 我们把对象(变量)从内存中编程可存储或传输的过程称之为序列化,在python中称为pickle,其他语言称之为serialization ,marshalling ,flatter ...
- 报错 ———— Cause: org.apache.ibatis.builder.BuilderException: Error creating document instance. Cause: org.xml.sax.SAXParseException; lineNumber: 2; columnNumber: 6; 不允许有匹配 "[xX][mM][lL]" 的处理指令目标。
报错 <?xml version="1.0" encoding="UTF-8"?> 必须是XML文件的第一个元素且前面不能空格. ### Erro ...
- .NET面试题集锦②(Part 二)
一.前言部分 文中的问题及答案多收集整理自网络,不保证100%准确,还望斟酌采纳. 1.实现产生一个int数组,长度为100,并向其中随机插入1-100,并且不能重复. ]; ArrayList my ...
- Xamarin.Forms 简介
An Introduction to Xamarin.Forms 来源:http://developer.xamarin.com/guides/cross-platform/xamarin-forms ...
- Spring MVC简介
Spring MVC简介 Spring MVC框架是有一个MVC框架,通过实现Model-View-Controller模式来很好地将数据.业务与展现进行分离.从这样一个角度来说,Spring MVC ...
- maven3.2.3+eclipse4.4+JDK1.8+win8.1_64bit环境搭建
--------------------------------------- 博文作者:迦壹 博客标题:win8.1_64bit+eclipse4.4+maven3.2.3+JDK1.8环境搭建 博 ...
- day--6_python常用模块
常用模块: time和datetime shutil模块 radom string shelve模块 xml处理 configparser处理 hashlib subprocess logging模块 ...
- 巩固一下:SpringMVC详细示例实战教程
一.SpringMVC基础入门,创建一个HelloWorld程序 1.首先,导入SpringMVC需要的jar包. 2.添加Web.xml配置文件中关于SpringMVC的配置 1 2 3 4 5 6 ...
- 史上最全最强SpringMVC详细示例实战教程
一.SpringMVC基础入门,创建一个HelloWorld程序 1.首先,导入SpringMVC需要的jar包. 2.添加Web.xml配置文件中关于SpringMVC的配置 1 2 3 4 5 6 ...
随机推荐
- UIView 判断是否visible
if(self.view == [(MyAppDelegate *)[[UIApplication sharedApplication] delegate].window.subviews objec ...
- redis :read error on connection
最近做了一个多人竞拍的小功能 因为以前没做过 所以踩了很多坑用的是 mysql + php + redis 实现的竞拍功能 这里先说一下踩得第二个坑redis 的原因 真是欲哭无泪 解决完一个 ...
- 模板—tarjan求割点
int dfn[MAXN],low[MAXN],cnt,root; bool iscut[MAXN]; void tarjan(int x) { dfn[x]=low[x]=++cnt; ; for( ...
- CSDN-Java培训 - 看看这次会有多少人跟风...
2019年5月8日,闲来无事(最近答辩还没事......),存个档. 看看这一波风口,记录互联网+教育.
- Project Euler Problem 21-Amicable numbers
先说最暴力的算法,直接对一万内的每个数字暴力分解因子(对每个数字的时间复杂度是O(sqrt(n)的),然后,用个数组记录下来因子和,然后寻找 亲密数. 好一点:要先打个素数表,然后对每个数字,分解素因 ...
- 2013-4-3 C#中alt键不是Keys.Alt 而是 Keys.LMenu
2013-4-3 C#中alt键不是Keys.Alt而是Keys.LMenu
- codeforces1238-div2
C 目前在h的高度,1~h每一个台阶要么处于out的状态,要么处于in的状态,问最少改变几个台阶的状态,使得能够从h的高度到0. 下降的唯一的方式,拉动lever,h-1的状态取反,下落的最大的高度不 ...
- setTimeout与setInterval有何区别?
①setTimeout和setInterval的语法相同.它们都有两个参数,一个是将要执行的代码字符串,还有一个是以毫秒为单位的时间间隔,当过了那个时间段之后就将执行那段代码. ②不过这两个函数还是有 ...
- C#的循环语句(一)
循环:反复执行某段代码. 循环四要素:初始条件,循环条件,循环体,状态改变.for(初始条件;循环条件;状态改变) {循环体} for 格式: for(int i=1/*初始条件*/;0<=10 ...
- C# const 和 readonly 有什么区别
在写常量的时候,是选择使用 const 还是 static readonly 是一个让人难以决定的问题,本文告诉大家这两个方法的区别 如果一个类有静态字段,会如何初始化 可以使用的方法有两个,第一个方 ...