python解析XML之ElementTree
#coding=utf-8
from xml.etree import ElementTree as ET
tree=ET.parse('test.xml')
root = tree.getroot()
#p=per.findall('caption') #for oneper in p:
# for child in oneper.getchildren():
# print child.tag,':',child.text
#for node in root.findall('caption'):
# print node.tag pe=root.findall('./item')
first = 0
second = 0
third = 0
for child in pe:
# for child in oneitem.getchildren():
print "the first layer-------------" print child.tag,'tag:',child.text,'attr:' ,child.attrib
second_child=child.findall('./item')
first+=1
for thirdchild in second_child:
print "the second layer=========="
gchild=thirdchild.findall('./item')
print thirdchild.tag,'tag:',thirdchild.text,'attr:' ,thirdchild.attrib
second+=1
# print gchild.tag,'-88-:',gchild.text
# print type(gchild)
for i in gchild:
print "the third layer 3333333333"
print "i in child %s"%i
print i.tag,'-5656-:',i.text,'attr:' ,i.attrib
third+=1 print "first =%d\n"%first
print "second=%d\n"%second
print "third =%d\n"%third
<?xml version="1.0" encoding="utf-8"?> <catalog>
<service>4</service>
<item id="1">
<item id="11">
<caption>c1</caption>
<caption>c2</caption>
<caption>c3</caption>
</item>
</item> <item id="9">
<item id="99">
<item id="999">
</item>
</item>
</item>
<item id="8">
<item id="88">
<item id="888">
</item>
</item>
</item>
<item id="2">
<caption> </caption>
</item>
<item id="3">
<caption>
<item>itemc4</item>
<book>Zope2</book>
</caption>
</item> <item id="4">
<caption>c5</caption>
</item> <item id="5">
<caption>c6</caption>
<caption>c7</caption>
</item> <item>item6</item>
<item>item7</item> </catalog>
python解析XML之ElementTree的更多相关文章
- python 解析xml
在工作中很多时候都要用到xml,使用这个时候难免会设计到解析他,然后就研究了一下python解析xml问题,看了很多东西,python有很多解析xml的包,但是也折腾我好一段时间,最后选择了这个方法. ...
- Python 解析 XML 文件生成 HTML
XML文件result.xml,内容如下: <ccm> <metric> <complexity>1</complexity> <unit> ...
- 横向对比分析Python解析XML的四种方式
横向对比分析Python解析XML的四种方式 在最初学习PYTHON的时候,只知道有DOM和SAX两种解析方法,但是其效率都不够理想,由于需要处理的文件数量太大,这两种方式耗时太高无法接受. 在网络搜 ...
- python 解析XML python模块xml.dom解析xml实例代码
分享下python中使用模块xml.dom解析xml文件的实例代码,学习下python解析xml文件的方法. 原文转自:http://www.jbxue.com/article/16587.html ...
- python解析xml模块封装代码
在python中解析xml文件的模块用法,以及对模块封装的方法.原文转自:http://www.jbxue.com/article/16586.html 有如下的xml文件:<?xml vers ...
- python解析xml之lxml
虽然python解析xml的库很多,但是,由于lxml在底层是用C语言实现的,所以lxml在速度上有明显优势.除了速度上的优势,lxml在使用方面,易用性也非常好.这里将以下面的xml数据为例,介绍l ...
- python解析xml
python解析xml import xml.dom.minidom as minidom dom = minidom.parse("aa.xml") root = dom.get ...
- Python 解析XML实例(xml.sax)
已知movies.xml <collection shelf="New Arrivals"> <movie title="Enemy Behind&qu ...
- python解析xml文件时使用ElementTree和cElementTree的不同点;iter
在python中,解析xml文件时,会选用ElementTree或者cElementTree,那么两者有什么不同呢? 1.cElementTree速度上要比ElementTree快,比较cElemen ...
随机推荐
- POJ题目(转)
http://www.cnblogs.com/kuangbin/archive/2011/07/29/2120667.html 初期:一.基本算法: (1)枚举. (poj1753,poj29 ...
- Tomcat基础
一.Tomcat体系结构 tomcat应该是java程序员最熟悉的servlet容器了,作为一个用java实现的web应用程序服务器,下图是tomcat的体系结构 一个常见的Tomcat配置文件以下x ...
- 深入JVM锁机制2-Lock
前文(深入JVM锁机制-synchronized)分析了JVM中的synchronized实现,本文继续分析JVM中的另一种锁Lock的实现.与synchronized不同的是,Lock完全用Java ...
- pc app 桌面打包
进入 http://nwjs.io/ 下载 创建web项目,在项目根目录 创建文件package.json并填写 1 2 3 4 5 6 7 { "name": " ...
- chapter8_1 编译执行和错误
1.编译 前面介绍的,dofile是一个运行lua代码块的基本操作,实际上它是一个辅助函数. loadfile才真正做了核心的工作.dofile(打开文件,执行里面的代码块)和loadfile(从文件 ...
- Openjudge-计算概论(A)-单词翻转
描述: 输入一个句子(一行),将句子中的每一个单词翻转后输出. 输入只有一行,为一个字符串,不超过500个字符.单词之间以空格隔开.输出翻转每一个单词后的字符串,单词之间的空格需与原文一致. 样例输入 ...
- python读取bin文件并下发串口
# coding:utf-8import time, serialfrom struct import *import binascii file = open('E:\\1.bin', 'rb')i ...
- OpenStack回顾随笔(第一章)
1. OpenStack历史 OpenStack前身是NASA和Rackspace合作的项目,2010年开源,与其他主流开源云管理系统:Citrix的Cloudstack和桉树的OpenNebu ...
- Tenured 区并发垃圾回收器CMS介绍
当使用CMS收集器时,当开始进行收集时,old代的收集过程如下所示:1,首先jvm根据-XX:CMSInitiatingOccupancyFraction,-XX:+UseCMSInitiatingO ...
- CentOS 6.4 系统下的MySQL的主从库配置
首先了解到一. 二一.(MySQL下创建用户并且赋予权限)root用户创建yong用户的SQL语句 CREATE USER 'yong'@'localhost' IDENTIFIED BY 'yong ...