一、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>

二、Python使用xml

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("xmltest.xml")
root = tree.getroot()
print(root.tag) #遍历xml文档
for child in root:
print('========>', child.tag, child.attrib, child.attrib['name'])
for i in child:
print(i.tag, i.attrib, 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')
node.set('version', '1.0')
tree.write('test.xml') #删除node
for country in root.findall('country'):
rank = int(country.find('rank').text)
if rank > 50:
root.remove(country) tree.write('output.xml') #在country内添加(append)节点year2
import xml.etree.ElementTree as ET
tree = ET.parse("a.xml")
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 = '新年'
year2.attrib = {'update': 'yes'}
country.append(year2) #往country节点下添加子节点 tree.write('a.xml.swap')

三、自己创建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 = '33'
name2 = ET.SubElement(new_xml, "name", attrib={"enrolled": "no"})
age = ET.SubElement(name2, "age")
age.text = '19' et = ET.ElementTree(new_xml) #生成文档对象
et.write("test.xml", encoding="utf-8", xml_declaration=True) ET.dump(new_xml) #打印生成的格式

最新文章

  1. c#中get set 的使用
  2. DelphiXE2 DataSnap开发技巧收集
  3. SSH框架搭建详解 及 乱码处理
  4. SqlServer 的提示符(Option/With等提示符)不是什么时候都可以用的
  5. PHP Simple HTML DOM 使用
  6. Oracle包的概念
  7. 不一样的味道--Html和Xml解析、格式、遍历
  8. 【Hexo】(一)使用HEXO配置环境,创建Hello World
  9. qt中的udp编程
  10. 用VSCode开发一个基于asp.net core 2.0/sql server linux(docker)/ng5/bs4的项目(3)
  11. 微信公众号开发C#系列-5、用户和用户组管理-支持同步
  12. SqlServer无备份下误删数据恢复
  13. SpringCloud Ribbon的分析
  14. MySQL用户授权【转】
  15. Hyper-V 与 VMware 和 vbox 的不兼容
  16. OOP⑶
  17. vue.js实战(文摘)
  18. webservice 客户端调用
  19. MySQL中MyISAM与InnoDB区别及选择,mysql添加外键
  20. requests.session之set trust_env to disable environment searches for proxies

热门文章

  1. swift声明属性为某个类型同时遵循某协议
  2. java类成员的默认可访问性是什么?你猜
  3. SQLSERVER预读逻辑读物理读
  4. MySQL数据库:子查询的应用
  5. 「Shimo使用指南」mac支持pptp协议的小软件
  6. Java Swing 图形界面开发
  7. Java程序远程无法执行nohup命令
  8. 多对多表结构的设计ManyToManyField(不会生成某一列、生成一张表):
  9. 使用原生代码实现一个Events模块,可以实现自定义事件的订阅、触发、移除功能
  10. Ubuntu16.04VIM无法补全错误记录