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

import xml.etree.ElementTree as ET  

tree = ET.parse('xmltest.xml')
root = tree.getroot()
print(root.tag)
# 输出
# data # 遍历xml文档
for child in root:
print(child.tag,child.attrib)
# 输出
# country {'name': 'Liechtenstein'}
# country {'name': 'Singapore'}
# country {'name': 'Panama'} for node in root:
for child in node:
print(child.tag,child.text)
# 输出
# rank 2
# year 2008
# gdppc 141100
# neighbor None
# neighbor None # rank 5
# year 2011
# gdppc 59900
# neighbor None # rank 69
# year 2011
# gdppc 13600
# neighbor None
# neighbor None # 只遍历year节点
for node in root.iter('year'):
print(node.tag,node.text) # 输出
# year 2008
# year 2011
# year 2011

  修改和删除xml文档

# 修改
for node in root.iter('year'):
new_year = int(node.text) + 1
node.text = str(new_year)
node.set('update','yes')
tree.write('xmltest.xml') # 只遍历year节点
for node in root.iter('year'):
print(node.tag,node.text)
# 输出
# year 2009
# year 2012
# year 2012
# 删除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={'enrolld': 'yes'})
age = ET.SubElement(name,'age',attrib={'checed': 'no'})
sex = ET.SubElement(name,'sex')
sex.text = '男' name2 = ET.SubElement(new_xml,'name',attrib={'enrolld': '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) #打印生成格式

20180209-xml模块的更多相关文章

  1. oop、configparser、xml模块

    本节大纲:一:在python中,有两种编程思想.1:函数式编程.2:oop.无论是函数式编程还是oop都是相辅相成.并不是说oop比函数式编程就好.各有各的优缺点.在其他语言中java等只能以面向对象 ...

  2. python解析xml模块封装代码

    在python中解析xml文件的模块用法,以及对模块封装的方法.原文转自:http://www.jbxue.com/article/16586.html 有如下的xml文件:<?xml vers ...

  3. s14 第5天 时间模块 随机模块 String模块 shutil模块(文件操作) 文件压缩(zipfile和tarfile)shelve模块 XML模块 ConfigParser配置文件操作模块 hashlib散列模块 Subprocess模块(调用shell) logging模块 正则表达式模块 r字符串和转译

    时间模块 time datatime time.clock(2.7) time.process_time(3.3) 测量处理器运算时间,不包括sleep时间 time.altzone 返回与UTC时间 ...

  4. python全栈开发-hashlib模块(数据加密)、suprocess模块、xml模块

    一.hashlib模块 1.什么叫hash:hash是一种算法(3.x里代替了md5模块和sha模块,主要提供 SHA1, SHA224, SHA256, SHA384, SHA512 ,MD5 算法 ...

  5. 【python标准库模块五】Xml模块学习

    Xml模块 xml本身是一种格式规范,是一种包含了数据以及数据说明的文本格式规范.在json没有兴起之前各行各业进行数据交换的时候用的就是这个.目前在金融行业也在广泛在运用. 举个简单的例子,xml是 ...

  6. Python xml 模块

    Python xml 模块 TOC 什么是xml? xml和json的区别 xml现今的应用 xml的解析方式 xml.etree.ElementTree SAX(xml.parsers.expat) ...

  7. python之xml模块

    # XML 模块的操作参考链接 # http://www.cnblogs.com/yuanchenqi/articles/5732581.html

  8. hashlib,hmac,subprocess,configparser,xlrd,xlwt,xml模块基本功能

    hashlib模块:加密 import hashlib# 基本使用cipher = hashlib.md5('需要加密的数据的二进制形式'.encode('utf-8'))print(cipher.h ...

  9. python configparse模块&xml模块

    configparse模块 用于生成和修改常见配置文档,当前模块的名称在 python 3.x 版本中变更为 configparser. [DEFAULT] serveraliveinterval = ...

  10. xml模块、项目开发过程

    一.XML模块 xml指的是可扩展标记语言,是一种定义电子文档结构和描述的语言,可以用来标记数据.定义数据类型. 什么时候用xml? 当需要自定义文档结构时,使用xml.在java中经常会使用xml来 ...

随机推荐

  1. LOJ 2840「JOISC 2018 Day 4」糖

    有趣的脑子题(可惜我没有脑子 好像也可以称为模拟费用流(? 我们考虑用链表维护这个东西 再把贡献扔到堆里贪心就好了 大概就是类似于有反悔机制的贪心?我们相当于把选中的一个打上一个-v的tag然后如果选 ...

  2. final、finally、 finalize区别

    原创转载请注明出处:https://www.cnblogs.com/agilestyle/p/11444366.html final final可以用来修饰类.方法.变量,分别有不同的意义,final ...

  3. Anacond的介绍

    Anacond的介绍 Anaconda指的是一个开源的Python发行版本,其包含了conda.Python等180多个科学包及其依赖项. 因为包含了大量的科学包,Anaconda 的下载文件比较大( ...

  4. [POJ 1911] 棋盘

    问题描述 将一个8*8的棋盘进行如下分割:将原棋盘割下一块矩形棋盘并使剩下部分也是矩形,再将剩下的部分继续如此分割,这样割了(n-1)次后,连同最后剩下的矩形棋盘共有n块矩形棋盘.(每次切割都只能沿着 ...

  5. MVC 无法将带 [] 的索引应用于“System.Dynamic.DynamicObject”类型的表达式

    无法将带 [] 的索引应用于“System.Dynamic.DynamicObject”类型的表达式 说明: 执行当前 Web 请求期间,出现未经处理的异常.请检查堆栈跟踪信息,以了解有关该错误以及代 ...

  6. php常用端口号

    常见端口号 Nginx 80 Nginx (“engine x”) 是一个高性能的HTTP和反向代理服务器,也是一个IMAP/POP3/SMTP服务器. MySQL 3306 MySQL是一种关系数据 ...

  7. execute、executeQuery和executeUpdate之间的区别 转

    转:http://blog.csdn.net/colin_fantasy/article/details/3898070 execute.executeQuery和executeUpdate之间的区别 ...

  8. apache主要配置详解

    1. # Deny access to the entirety of your server's filesystem. You must # explicitly permit access to ...

  9. .Net-WCF-图书:《WCF编程》

    ylbtech-.Net-WCF-图书:<WCF编程> <WCF编程>是2008年1月机械工业出版社出版的图书,作者是Juval Lowy.Clemens Vasters. 1 ...

  10. day11—前端学习之我不想看书

    转行学开发,代码100天——2018-03-27 今天是前端学习,博客记录见证的第11天,按理说还要继续写一写代码相关的内容. 但由于今天的代码实践都是一些基础知识,还是想谈一谈关于编程方面看书的事情 ...