---恢复内容开始---

导入数据(读文件和读字符串)

本地文件 country_data.xml

<?xml version="1.0"?>
<data>
<country name="Liechtenstein">
<rank>1</rank>
<year>2008</year>
<gdppc>141100</gdppc>
<neighbor name="Austria" direction="E"/>
<neighbor name="Switzerland" direction="W"/>
</country>
<country name="Singapore">
<rank>4</rank>
<year>2011</year>
<gdppc>59900</gdppc>
<neighbor name="Malaysia" direction="N"/>
</country>
<country name="Panama">
<rank>68</rank>
<year>2011</year>
<gdppc>13600</gdppc>
<neighbor name="Costa Rica" direction="W"/>
<neighbor name="Colombia" direction="E"/>
</country>
</data>

从文件中读取 xml

import xml.etree.ElementTree as ET
tree = ET.parse('country_data.xml')
root = tree.getroot()

从字符串中读取 xml

root = ET.fromstring(country_data_as_string)

Element.tag 、Element.text 和 Element.attributes

# root.tag 读取 tag 名
# root.attrib 读取 attributes
# root[0][1].rank 读取文本值
print root.tag #输出根节点元素<data>的tag名:data
print root.attrib #输出根节点元素<data>的attributes(空值):{}
print root[0][2].text #输出第一个<country>的子元素<gdppc>的文本值:141100

Element.iter()

# root.iter('neighbor')查找所有子孙元素
for neighbor in root.iter('neighbor'):
print neighbor.attrib

find() 和 findall()

# find()返回第一个子元素
print root.find('country')
# fund() 返回所有子元素
print root.findall('country')

照搬手册:

https://docs.python.org/2/library/xml.etree.elementtree.html

[python 学习] 使用 xml.etree.ElementTree 模块处理 XML的更多相关文章

  1. python xml.etree.ElementTree模块

    使用的XML文件如下:file.xml <?xml version="1.0"?> <data name="ming"> <cou ...

  2. xml.etree.ElementTree模块的封装

    转载:https://www.cnblogs.com/hongten/p/hongten_python_xml_etree_elementtree.html 1 # -*- coding: utf-8 ...

  3. python xml.etree ElementTree解析 编辑 xml

    python有很多种xml解析方式,不过感觉etree的ElementTree 用起来最方便. #coding=utf-8 from xml.etree import ElementTree impo ...

  4. Python 标准库之 xml.etree.ElementTree

    Python 标准库之 xml.etree.ElementTree Python中有多种xml处理API,常用的有xml.dom.*模块.xml.sax.*模块.xml.parser.expat模块和 ...

  5. python 之xml.etree.ElementTree

    Element类型是一种灵活的容器对象,用于在内存中存储结构化数据. [注意]xml.etree.ElementTree模块在应对恶意结构数据时显得并不安全. 每个element对象都具有以下属性: ...

  6. Python中xml.etree.ElementTree读写xml文件实例

    import osimport xml.etree.ElementTree as ET'''Python 标准库中,提供了6种可以用于处理XML的包,本文举实例说明第6种1.xml.dom2.xml. ...

  7. python模块:xml.etree.ElementTree

    """Lightweight XML support for Python. XML is an inherently hierarchical data format, ...

  8. python模块之xml.etree.ElementTree

    xml.etree.ElementTree用于解析和构建XML文件 <?xml version="1.0"?> <data> <country nam ...

  9. Python学习第十二课——json&pickle&XML模块&OS模块

    json模块 import json dic={'name':'hanhan'} i=8 s='hello' l=[11,22] data=json.dumps(dic) #json.dumps() ...

随机推荐

  1. 利用域凭据:解密GPP中的管理员密码

    在利用域凭据过程中,除了通过Mimikatz和WCE从内存读取明文密码外,还可以通过域共享文件夹SYSVOL组策略文件获取哈希码. 组策略首选项(Group Policy Preference, GP ...

  2. Java数据结构与算法(1):线性表

    线性表是一种简单的数据类型,它是具有相同类型的n个数据元素组成的有限序列.形如如A0,A1,...,An-1.大小为0的表为空表,称Ai后继Ai-1,并称Ai-1前驱Ai. printList打印出表 ...

  3. springboot定时任务出错 Unexpected use of scheduler.

    java.lang.IllegalStateException: Unexpected use of scheduler. 在启动类加: @Bean public ThreadPoolTaskSche ...

  4. Django路由小知识

    from django.urls import path,re_path from app01 import views urlpatterns = [ re_path(r'^articles/200 ...

  5. 使用@Value注解对bean进行属性注入

    使用@Value注解,可以有三种属性注入的方式: 1. 使用字面量注入 2. 使用EL表达式注入 3. 使用占位符注入 import org.springframework.beans.factory ...

  6. Linux_FTP服务器

    目录 目录 FTP FTP Server FTP configuration Global config Anonymous user FTP Config Virtual user FTP Loca ...

  7. VMware 接入 Openstack — 使用 Openstack 创建 vCenter 虚拟机

    目录 目录 软件环境 前言 Openstack 接口驱动 使用 KVM 在 Compute Node 上创建虚拟机的流程 使用 VCDirver 在 vCenter 上创建虚拟机的流程 配置 vCen ...

  8. template标签就相当于React中的fragment

    template标签就相当于React中的fragment

  9. gradle implementation runtimeOnly 和api 区别

    implementation  不对外开发,只是本项目依赖. runtimeOnly 运行时才依赖 api 可以传递依赖,别的项目也可以依赖api的jar包.

  10. 5分钟连续出现某现象+微信模板消息提醒 PHP

    需求场景:用电插座电流连续出现5次电流过高(大于 3A)后停止用电服务,前四次发送电流过高提醒,最后一次发送结束用电服务提醒 思路: Redis  key 设为:插座编号+user户编号  value ...