19.7.1 教程

这是一个简短的教程使用xml.etree.ElementTree(简称为et)。目标是展示一些构建模块和模块的基本概念

9.7.1.1. XML tree and elements

XML是一种固有的层次化的数据格式,最自然的方式来表示这是树。为此ET有两个方法——ElementTree代表整个XML文档树,Element表示这个树中的一个节点。与整个文档交互(阅读和写作/文件)通常是在ElementTree水平。与一个XML元素及其子元素是元素级别上完成的。

9.7.1.2

xml文件,保存到本地test.xml

<?xml version="1.0" encoding="utf-8"?>
<request>
<functionID>subPackageInfo</functionID>
<time>2014-02-10 15:10:50</time>
<packageList>
<packageInfo>
<orderId id=''>22088317130</orderId>
<expressName id=''>ems</expressName>
<expressTel>01</expressTel>
<expressNo>0001</expressNo>
<productId>1001173023</productId>
<allotQuatity>5</allotQuatity>
<outOfStockQuatity>0</outOfStockQuatity>
<promotionID></promotionID>
</packageInfo> <packageInfo>
<orderId id=''>22088317130</orderId>
<expressName id=''>23</expressName>
<expressTel>010-55675233</expressTel>
<expressNo>0002</expressNo>
<productId>1001173123</productId>
<allotQuatity>5</allotQuatity>
<outOfStockQuatity>0</outOfStockQuatity>
<promotionID>-1</promotionID>
</packageInfo> <packageInfo>
<orderId>22088317130</orderId>
<expressName>EMS</expressName>
<expressTel>010-55675233</expressTel>
<expressNo>0003</expressNo>
<productId>1001173223</productId>
<allotQuatity>0</allotQuatity>
<outOfStockQuatity>5</outOfStockQuatity>
<promotionID>-1</promotionID>
</packageInfo> </packageList>
</request>

解析xml文件

from xml.etree import ElementTree
tree=ElementTree.parse('test.xml')
#tree= ET.fromstring(country_data_as_string) #fromstring解释字符串,即country_data_as_string为读取xml的字符串
getroot()返回树结构的根元素
get 获取元素的标签
>>> root=tree.getroot()
>>> root.tag
'request'

find和findall,如果参数是元素名称的话只能查找当前节点的下一层节点,用法下面再介绍

list=root.find('packageList')
infos=list.findall('packageInfo')

查找packageInfo下面的orderId节点的文本

>>> for i in infos:
print i.find('orderId').text 22088317130
22088317130
22088317130

find()

1.root.find('packageList')
只匹配root节点下最上层元素,不匹配嵌入另一个元素的元素类型packageList
2.root.find('packageList/packageInfo')直接找到packageInfo节点
3.a=root.findall('*/packageInfo')
>>> for i in a:print i.tag packageInfo
packageInfo
packageInfo
4.我称为xpath方法
a=root.findall('.//orderId')
>>> for i in a:print i.text 22088317130
22088317130
22088317130
>>> a=root.findall('.//orderId[@id="9001"]')
>>> a[0].text
''

python xml包 xml.etree.ElementTree使用记录的更多相关文章

  1. python 解析xml遇到xml.etree.ElementTree.ParseError: not well-formed (invalid token): line 4, column 34

    在调试数字驱动用xml文件的方式时,包含读取xml文件的步骤,运行程序报错: d:\test\0629>python XmlUtil.pyTraceback (most recent call ...

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

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

  3. python标准库xml.etree.ElementTree的bug

    使用python生成或者解析xml的方法用的最多的可能就数python标准库xml.etree.ElementTree和lxml了,在某些环境下使用xml.etree.ElementTree更方便一些 ...

  4. python模块:xml.etree.ElementTree

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

  5. python xml.etree.ElementTree模块

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

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

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

  7. python解析xml文件之xml.etree.cElementTree和xml.etree.ElementTree区别和基本使用

    1.解析速度:ElementTree在 Python 标准库中有两种实现.一种是纯 Python 实现例如 xml.etree.ElementTree ,另外一种是速度快一点的 xml.etree.c ...

  8. [python 学习] 使用 xml.etree.ElementTree 模块处理 XML

    ---恢复内容开始--- 导入数据(读文件和读字符串) 本地文件 country_data.xml <?xml version="1.0"?> <data> ...

  9. python 之xml.etree.ElementTree

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

随机推荐

  1. jar -cmf file1 file2 file3命令

    jar -cmf file1 file2 file3中的参数c.m.f和file1.file2.file3是一一对应的. 也就是说,file1是输出的.jar文件,file2是往META-INF/MA ...

  2. SNMP 监控方式的配置

    由于某些设备并不能安装 Agent,或者不方便安装 Agent 等因素,将采用 SNMP 方式进行监控 1.Linux 配置 SNMP [root@crazy-acong ~]# yum -y ins ...

  3. Python学习笔记_Python基础

    Python 基础 语句和语法 凝视 继续 代码组 代码的缩进 在一行书写多个语句 模块 变量赋值 赋值操作符 增量赋值 多重赋值 多元赋值 python编写的基本风格 模块的结构和布局 内存管理 变 ...

  4. cocos2d-x 源代码分析 : control 源代码分析 ( 控制类组件 controlButton)

    源代码版本号来自3.1rc 转载请注明 cocos2d-x源代码分析总文件夹 http://blog.csdn.net/u011225840/article/details/31743129 1.继承 ...

  5. 了解CentOS服务器的基本信息

    简单描述了如何从CPU.内存.硬盘性能.负载方面去了解自己工作的服务器性能.这个很重要,必须了解机器的方方面面才能提高在自己运维工作效率. 一.查看linux服务器cpu详情 查看物理cpu个数: [ ...

  6. 淘宝开源平台(taobao-code)使用

    偶尔之下翻到的这个东西,瞬间觉得足以解决自己在开发过程中的版本控制问题.就注册了一个试试.先是在度娘上搜寻“淘code”,进入官网之后直接注册.然后构建自己的项目,上传代码就OK了. 一.搜寻“淘co ...

  7. MyBatis:学习笔记(4)——动态SQL

    MyBatis:学习笔记(4)——动态SQL 如果使用JDBC或者其他框架,很多时候需要你根据需求手动拼装SQL语句,这是一件非常麻烦的事情.MyBatis提供了对SQL语句动态的组装能力,而且他只有 ...

  8. PAT 天梯赛 【】 L3-015. 球队“食物链” 【BFS+剪枝】

    题目链接 https://www.patest.cn/contests/gplt/L3-015 思路 用一个 数组标记 胜负 每次输入一行字符串 然后遍历 如果 碰到 W 那么 vis[i][j] = ...

  9. PIG执行MR时报Connection refused错误

    原因是jobhistory没有启动,其启动脚本位于hadoop/sbin目录下 启动命令如下 mr-jobhistory-daemon.sh start historyserver

  10. Data Structure Binary Tree: Check if a given Binary Tree is SumTree

    http://www.geeksforgeeks.org/check-if-a-given-binary-tree-is-sumtree/ #include <iostream> #inc ...