python之xml模块

news/2024/6/17 13:41:20

xml模块

xml是实现不同语言或程序之间进行数据交换的协议,跟json差不多,但json使用起来更简单,不过,古时候,在json还没诞生的黑暗年代,大家只能选择用xml呀,至今很多传统公司如金融行业的很多系统的接口还主要是xml。

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

2、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')

要点

  • parse(‘file’) 解析文件
  • getroot() 获取根节点
  • 根节点可遍历:for child in root: child.tag,child.attrib ;其中child.tag获取子节点标签,child.attrib 获取子节点属性值
  • 上面的child 也是可遍历对象: for i in child: i.tag ;i.text 其中i.tag 获取标签,i.text 获取文本
  • iter() 过滤字段
  • findall() 匹配字段

3、自己创建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) #打印生成的格式

http://www.niftyadmin.cn/n/852413.html

相关文章

2019-12-2:回收站选址【map】【set】

问题描述 试题编号&#xff1a;201912-2试题名称&#xff1a;回收站选址时间限制&#xff1a;1.0s内存限制&#xff1a;512.0MB问题描述&#xff1a; 输入的每个坐标都是唯一的&#xff0c;可以用map #include<iostream> #include<map> #include<cst…

ASP.NET MVC 5 - 视图

原文:ASP.NET MVC 5 - 视图在本节中&#xff0c;你要去修改HelloWorldController类&#xff0c;使用视图模板文件&#xff0c;在干净利索地封装的过程中&#xff1a;客户端浏览器生成HTML。 您将创建一个视图模板文件&#xff0c;其中使用了ASP.NET MVC 3所引入的Razor视图引擎…

HTML基础(超链接标签 URL)

超链接标签 <a href"www.baidu.com">百度一下你就知道[此处可以文本也可以是图片]</a> <a href"hypelink.html" target"_blank">返回</a> <a href"http://www.baidu.com" target"_blank"> &l…

力荐51CTO网站http://down.51cto.com/

力荐51CTO网站 我以一个普通网民的身份力荐本网站&#xff0c;我发现本站技术性&#xff0c;功能齐全&#xff0c;分类明显 最主要的是&#xff0c;有些不常用的工具软件本站却有&#xff0c;其它网站根本找不到&#xff0c; 所以我个人力荐本站&#xff0c;希望大家们支持&…

python之configparser模块

configparser模块 格式 来看一个好多软件的常见文档格式如下&#xff1a; [DEFAULT] ServerAliveInterval 45 Compression yes CompressionLevel 9 ForwardX11 yes[bitbucket.org] User hg[topsecret.server.com] Port 50022 ForwardX11 no如果想用python生成一个这样…

【AutoMapper官方文档】DTO与Domin Model相互转换(上)

写在前面 AutoMapper目录&#xff1a; 【AutoMapper官方文档】DTO与Domin Model相互转换&#xff08;上&#xff09;【AutoMapper官方文档】DTO与Domin Model相互转换&#xff08;中&#xff09;【AutoMapper官方文档】DTO与Domin Model相互转换&#xff08;下&#xff09;未完…

python之hashlib模块

hashlib模块 用于加密相关的操作&#xff0c;3.x里代替了md5模块和sha模块&#xff0c;主要提供 SHA1, SHA224, SHA256, SHA384, SHA512 &#xff0c;MD5 算法 import hashlibmhashlib.md5()# mhashlib.sha256()m.update(hello.encode(utf8)) print(m.hexdigest()) #5d41402…

zabbix web场景模拟监控配置

一,zabbix2.2.3 VMware Vsphere exsi监控配置步骤,1,添加监控主机2,添加聚集macro;{$PASSWORD} yoodo.com{$URL} http://ip/sdk{$USERNAME} root3,关联监控模板模板用Template Virt VMware;二,web应用场景监控配置1, 2.2.0后web监控不在web中直接添加web Scenario选定主机了…