转载: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的更多相关文章

  1. python序列化及其相关模块(json,pickle,shelve,xml)详解

    什么是序列化对象? 我们把对象(变量)从内存中编程可存储或传输的过程称之为序列化,在python中称为pickle,其他语言称之为serialization ,marshalling ,flatter ...

  2. 报错 ———— 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 ...

  3. .NET面试题集锦②(Part 二)

    一.前言部分 文中的问题及答案多收集整理自网络,不保证100%准确,还望斟酌采纳. 1.实现产生一个int数组,长度为100,并向其中随机插入1-100,并且不能重复. ]; ArrayList my ...

  4. Xamarin.Forms 简介

    An Introduction to Xamarin.Forms 来源:http://developer.xamarin.com/guides/cross-platform/xamarin-forms ...

  5. Spring MVC简介

    Spring MVC简介 Spring MVC框架是有一个MVC框架,通过实现Model-View-Controller模式来很好地将数据.业务与展现进行分离.从这样一个角度来说,Spring MVC ...

  6. maven3.2.3+eclipse4.4+JDK1.8+win8.1_64bit环境搭建

    --------------------------------------- 博文作者:迦壹 博客标题:win8.1_64bit+eclipse4.4+maven3.2.3+JDK1.8环境搭建 博 ...

  7. day--6_python常用模块

    常用模块: time和datetime shutil模块 radom string shelve模块 xml处理 configparser处理 hashlib subprocess logging模块 ...

  8. 巩固一下:SpringMVC详细示例实战教程

    一.SpringMVC基础入门,创建一个HelloWorld程序 1.首先,导入SpringMVC需要的jar包. 2.添加Web.xml配置文件中关于SpringMVC的配置 1 2 3 4 5 6 ...

  9. 史上最全最强SpringMVC详细示例实战教程

    一.SpringMVC基础入门,创建一个HelloWorld程序 1.首先,导入SpringMVC需要的jar包. 2.添加Web.xml配置文件中关于SpringMVC的配置 1 2 3 4 5 6 ...

随机推荐

  1. 2018-8-10-WPF-编译为-AnyCPU-和-x86-有什么区别

    title author date CreateTime categories WPF 编译为 AnyCPU 和 x86 有什么区别 lindexi 2018-08-10 19:16:53 +0800 ...

  2. oracle交互命令

    (1)说明:可以替代变量,而该变量在执行时,需要用户输入. sql>select * from emp where job=’&job’; (2)edit  说明:该命令可以编辑指定的s ...

  3. java代码简单实现栈

    1. 基于数组简单实现 /** * @author <a herf="mailto:yanwu0527@163.com">XuBaofeng</a> * @ ...

  4. 关于使用JavaMail发送邮件

    import lombok.extern.slf4j.Slf4j; import org.apache.commons.lang3.StringUtils; import javax.activati ...

  5. 2019-7-22-Roslyn-获得-sln-文件所在的文件夹

    title author date CreateTime categories Roslyn 获得 sln 文件所在的文件夹 lindexi 2019-07-22 08:57:14 +0800 201 ...

  6. Vue电商后台管理系统项目第1天-基本环境搭建&登录功能

    基本环境搭建完成 安装npm包:npm -S i vue vue-router axios element-ui 配置Eslint: 打开设置,搜索Eslint拓展,然后将下面代码覆盖进去即可 { , ...

  7. 设计模式 - 工厂模式(factory pattern) 具体解释

    版权声明:本文为博主原创文章,未经博主同意不得转载. https://blog.csdn.net/u012515223/article/details/27081511 工厂模式(factory pa ...

  8. 洛谷P1616 疯狂的采药

    //完全背包 #include<bits/stdc++.h> using namespace std; ; ; int n,m,v[maxn],w[maxn],f[maxv]; int m ...

  9. EF的多线程与分库架构设计实现(2)

    距离上次讲解EF6分库架构(https://www.cnblogs.com/gbat/p/6374607.html)实现已经过去2年了..上次发出来后,一直到现在依然有很多人陆陆续续的加我好友,问我要 ...

  10. Java 简单校验框架

    数据校验框架现状 在我们的方法入口后面,难免会有如下样子的代码: result.setSuccess(false); if (StringUtils.isBlank(bizOrder.getThird ...