XML 文件:xmlparse.xml

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!DOCTYPE Model SYSTEM "/var/company/user/etc/user2017.dtd">
<Model version="1" importVersion="16.2">
<Create>
<Company name="Google.inc">
<Division sourceType="Personnel">
<UserName string="John Smith" />
<Sex string="Male" />
<Age int="30" />
<HireDate string="2009-07-01" />
<Station sting="Manager" />
<isMarry boolen="True" />
</Division>
<Division sourceType="Personnel">
<UserName string="Mary Smith" />
<Sex string="Female" />
<Age int="23" />
<HireDate string="2017-07-01" />
<Station string="Secretary" />
<isMarry boolen="False" />
</Division>
</Company>
<Company name="Baidu.inc">
<Division sourceType="Personnel">
<UserName string="Alice Wang" />
<Sex string="Female" />
<Age int="29" />
<HireDate string="2002-07-01" />
<Station string="HR Manager" />
<isMarry boolen="True" />
</Division>
<Division sourceType="Personnel">
<UserName string="Mark Zhou" />
<Sex string="Male" />
<Age int="20" />
<HireDate string="" />
<Station string="Intern" />
<isMarry boolen="False" />
</Division>
</Company>
</Create>
</Model>

解析XML文件并打印每个公司每个人的年龄:testparse.py

import xml.etree.ElementTree as ET

tree = ET.parse('xmlparse.xml')
root = tree.getroot() for Division in root.findall('.//Division'):
UserName = Division.find('UserName').attrib
Age = Division.find('Age').attrib
print UserName, Age

输出结果:

{'string': 'John Smith'} {'int': ''}
{'string': 'Mary Smith'} {'int': ''}
{'string': 'Alice Wang'} {'int': ''}
{'string': 'Mark Zhou'} {'int': ''}

[python 2.x] xml.etree.ElementTree module的更多相关文章

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

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

  2. python模块:xml.etree.ElementTree

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

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

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

  4. python模块之xml.etree.ElementTree

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

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

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

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

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

  7. 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 ...

  8. python xml.etree.ElementTree模块

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

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

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

随机推荐

  1. 给codeblocks的c编译选项添加c99标准

    在codeblocks的settings中选择 compiler and debugger,选择compile setting 在其中有other options,在里面写上-std=c99 这样就可 ...

  2. day40

    session / cookies : 会话跟踪技术是Cookie与Session 会话(Session)跟踪是Web程序中常用的技术,用来跟踪用户的整个会话. Cookie通过在客户端记录信息确定用 ...

  3. Windows(win2016、win2019、win10)在IIS下添加.NET Framework 3.5 NetFx3 失败 (状态为:0x800f0950)的解决办法

    今天一个客户自己的电脑安装了一个windows server 2016 想装一个IIS,程序一个C+写的ERP,NET是必然,NET4.7可以安装了,但就是3.5,如何也装不上,错误(状态为:0x80 ...

  4. OpenFOAM——前台阶

    本算例来自<ANSYS Fluid Dynamics Verification Manual>中的VMFL037:Turbulent Flow Over a Forward Facing ...

  5. [Gamma阶段]第五次Scrum Meeting

    Scrum Meeting博客目录 [Gamma阶段]第五次Scrum Meeting 基本信息 名称 时间 地点 时长 第五次Scrum Meeting 19/05/31 大运村寝室6楼 30min ...

  6. BaiduPCS-Go的安装及使用

    BaiduPCS-Go的安装及使用 linux下会提示输入验证码,浏览器打开验证码url,多输入几次 Contents [hide] 一. 软件下载及安装 二. 软件的使用 1. 账号登录与退出 2. ...

  7. Python80个练手项目列表

    原文地址:https://www.shiyanlou.com/questions/102676/?utm_source=baidu&utm_medium=cpc&utm_campaig ...

  8. 检查 chrome 插件是否存在

    你必须了解 chrome 插件开发才能阅读以下内容. 传送门: https://qa.1r1g.com/sf/ask/440544891/ 原理:页面 js 向 chrome 插件的 backgrou ...

  9. 【NWJS】解析node-webkit(NWJS)的打包和发布

    目录结构: contents structure [-] 下载和安装node-webkit 建立一个简单的WEB应用 生成EXE可执行文件 修改icon 封包 Enigma Virtual Box I ...

  10. [转]js判断数据类型的四种方法

    原文地址:https://www.cnblogs.com/crackedlove/p/10331317.html 1.typeof typeof是一个操作符,其右侧跟一个一元表达式,并返回这个表达式的 ...