python xml.etree.ElementTree解析xml文件获取节点
<?xml version = "1.0" encoding = "utf-8"?>
<root>
<body name="lyc">
<age>110</age>
</body>
<body name = "l" age = "10">
</body>
</root> ###################### #coding=UTF8 from xml.etree import ElementTree #xmlText = open("xml.txt").read()
#root = ElementTree.fromstring(xmlText) root = ElementTree.parse("xml.txt")
bodys = root.getiterator("body") #getiterator方法获取
print "getiterator"
print bodys
print dir(bodys[0])
print "attrib:",bodys[0].attrib
print "tag:",bodys[0].tag
print "text",bodys[0].text
#getchildren方法获取
print "getchildren"
children = bodys[0].getchildren()
print children
print "attrib:",children[0].attrib
print "tag:",children[0].tag
print "text:",children[0].text
#find
print "find"
children = root.find("body")
print children
print "attrib:",children.attrib
print "tag:",children.tag
print "text:",children.text
#findall
print "findall"
children = root.findall("body")
print children
print "attrib:",children[0].attrib
print "tag:",children[0].tag
print "text:",children[0].text
aa = xml.dom.minidom.parseString(response_res).documentElement.getElementsByTagName('string')[0].childNodes[0].data
doc = etree.XML(aa)
doc.xpath("//BoardData")[0].getchildren()[0].text
python xml.etree.ElementTree解析xml文件获取节点的更多相关文章
- python3.x中xml.etree.ElementTree解析xml举例
1.新建xml import xml.etree.ElementTree as ETa=ET.Element('elem')c=ET.SubElement(a,'child1')c.text=&quo ...
- Python中xml.etree.ElementTree读写xml文件实例
import osimport xml.etree.ElementTree as ET'''Python 标准库中,提供了6种可以用于处理XML的包,本文举实例说明第6种1.xml.dom2.xml. ...
- Python中使用ElementTree解析xml
在Python中,ElementTree是我们常用的一个解析XML的模块 1.导入ElementTree模块 from xml.etree import ElementTree as ET 2.初始化 ...
- python xml.etree ElementTree解析 编辑 xml
python有很多种xml解析方式,不过感觉etree的ElementTree 用起来最方便. #coding=utf-8 from xml.etree import ElementTree impo ...
- python解析xml文件之xml.etree.cElementTree和xml.etree.ElementTree区别和基本使用
1.解析速度:ElementTree在 Python 标准库中有两种实现.一种是纯 Python 实现例如 xml.etree.ElementTree ,另外一种是速度快一点的 xml.etree.c ...
- python XML文件解析:用ElementTree解析XML
Python标准库中,提供了ET的两种实现.一个是纯Python实现的xml.etree.ElementTree,另一个是速度更快的C语言实现xml.etree.cElementTree.请记住始终使 ...
- 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 ...
- [python]使用ElementTree解析XML【译】
19.7 The ElementTree XML API 源码:Lib/xml/etree/ElementTree.py Element类型是一个灵活的容器对象,设计出来是用于存储有层次的数据结构到内 ...
- ZH奶酪:Python使用ElementTree解析XML【译】
19.7. xml.etree.ElementTree — The ElementTree XML API 源代码: Lib/xml/etree/ElementTree.py Element类型是一种 ...
随机推荐
- shell 指定行插入
#如果知道行号可以用下面的方法 sed -i '88 r b.file' a.file #在a.txt的第88行插入文件b.txt awk '1;NR==88{system("cat ...
- AspNet5 Changes to [Activate] in beta-5
最近在看AspNet Core相关的文章,其中有个TagHelper,看上善若水的博客“关于TagHelper的那些事”,其中有一句 下面来自上善若水的博客原文: 我们知道ASP.NET 5实现了依赖 ...
- You are my brother NBUT - 1218
问题描述 Little A gets to know a new friend, Little B, recently. One day, they realize that they are fam ...
- 进入bios后没有usb启动项怎么办
开机按DEL进入BIOS(现在还这么说吧,不同的主板进入方法不太一样),找到BOOT选项. 选择Boot mood:legacy support(引导模式,逻辑支持) boot priorty:leg ...
- BZOJ 4247 挂饰(背包问题)
[题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=4247 [题目大意] JOI君有N个装在手机上的挂饰,编号为1...N. JOI君可以将 ...
- 【动态规划】Codeforces Round #406 (Div. 2) C.Berzerk
有向图博弈问题. 能转移到一个必败态的就是必胜态. 能转移到的全是必胜态的就是必败态. 转移的时候可以用队列维护. 可以看这个 http://www.cnblogs.com/quintessence/ ...
- 【tarjan+缩点】POJ1236[IOI1996]-Network of Schools
[题意] 见:http://blog.csdn.net/ascii991/article/details/7466278 [思路] 缩点+tarjan,思路也可以到上面的博客去看.(吐槽:这道题其实我 ...
- bzoj 1783: [Usaco2010 Jan]Taking Turns
1783: [Usaco2010 Jan]Taking Turns Description Farmer John has invented a new way of feeding his cows ...
- [HDU4729]An Easy Problem for Elfness
[HDU4729]An Easy Problem for Elfness 题目大意: 给你一棵\(n(n\le10^5)\)个点的树,树上每条边都有容量. \(m(m\le10^5)\)次询问,每次询 ...
- Java并发(四):happens-before
happens-before 一个操作执行的结果需要对另一个操作可见,那么这两个操作之间必须存在happens-before关系 happen-before原则是JMM中非常重要的原则,它是判断数据是 ...