ElementTree就像一个轻量级的DOM,具有方便友好的API。代码可用性好,速度快,消耗内存少,这里主要
介绍ElementTree。
下面是例子:(这是一个jmeter执行结果jtl文件)
kmtest071009.xm<?xml version="1.0" encoding="UTF-8"?>
<testResults version="1.2">
<httpSample t="636" lt="636" ts="1437376387035" s="true" lb="HTTP??" rc="200" rm="OK" tn="??? 1-81" dt="text" by="288"/>
<httpSample t="1184" lt="1184" ts="1437376386488" s="true" lb="HTTP??" rc="200" rm="OK" tn="??? 1-46" dt="text" by="288"/>
<httpSample t="964" lt="964" ts="1437376386708" s="true" lb="HTTP??" rc="200" rm="OK" tn="??? 1-8" dt="text" by="288"/>
<httpSample t="648" lt="648" ts="1437376387024" s="true" lb="HTTP??" rc="200" rm="OK" tn="??? 1-80" dt="text" by="288"/>
<httpSample t="701" lt="701" ts="1437376386971" s="true" lb="HTTP??" rc="200" rm="OK" tn="??? 1-73" dt="text" by="288"/>
<httpSample t="983" lt="983" ts="1437376386689" s="true" lb="HTTP??" rc="200" rm="OK" tn="??? 1-16" dt="text" by="288"/>
<httpSample t="951" lt="951" ts="1437376386721" s="true" lb="HTTP??" rc="200" rm="OK" tn="??? 1-20" dt="text" by="288"/>
<httpSample t="955" lt="955" ts="1437376386717" s="true" lb="HTTP??" rc="200" rm="OK" tn="??? 1-63" dt="text" by="288"/>
</testResults>
1.加载xml文件
加载XML文件共有2种方法,一是加载指定字符串,二是加载指定文件
2.获取element的方法
a) 通过getiterator
b) 过 getchildren
c) find方法
d) findall方法
python事例
#!/usr/bin/evn python
#coding:utf-8
try:
import xml.etree.cElementTree as ET
except ImportError:
import xml.etree.ElementTree as ET
import sys
try:
tree = ET.parse(r'kmtest071009.xml')
#tree = ET.parse(r'country.xml')
#root = ET.fromstring(country_string)
#tree = ET.parse(r'test.xml')
root = tree.getroot()
print root
print root.tag
print root.attrib
print "-----------"
#per=ET.parse(r'test.xml')
p=tree.findall('httpSample')
for x in p:
print "x:", x.attrib
print "new------"
#能取到
<httpSample t="955" lt="955" ts="1437376386717" s="true" lb="HTTP??" rc="200" rm="OK" tn="??? 1-63" dt="text" by="288"/> 这个中的值
for oneper in p:
#for child in oneper.getchildren():
#print child.tag,':',child.text
print 't:',oneper.get('t')
print 'tl:',oneper.get('tl')
print 'ts:',oneper.get('ts')
print 's:',oneper.get('s')
print 'lb:',oneper.get('lb')
print 'rc:',oneper.get('rc')
print 'rm:',oneper.get('rm')
print 'tn:',oneper.get('tn')
print 'dt:',oneper.get('dt')
print 'by',oneper.get('by')
print '############'
#children = httpSample[0].getchildren()
#print children
#print "tag:",children[0].tag
#print "text:",children[0][1].text
#print "-----------"
#print root[0][0].text
print "find"
children = root.find("httpSample")
print children
print "attrib:",children.attrib
print "tag:",children.tag
print "text:",children.text
#print "-------------1"
#print "attrib1:",children[0].attrib
print "--------------1"
#children = root.findall("httpSample/t")[0]
#print children
#print "attrib:",children[1].attrib
#print "tag:",children[1].tag
#print "text:",children[1].text
p = tree.findall("httpSample")
for x in p:
print "x.attrib:",x.attrib
#print "x.attrib.text:",x.attrib.text
#print "lt:",p.get('lt')
except Exception, e:
print "Error:cannot parse file:kmtest071009.xml."
sys.exit(1)
#print root.tag, "---", root.attrib
#for child in root:
#print child.tag, "---", child.attrib
#print child.attrib[0]
print "11111111"
#childs = item.getchildren()
#for subItem in childs:
#:print subItem.get("ts")
显示运行结果:
new------
t: 636
tl: None
ts: 1437376387035
s: true
lb: HTTP??
rc: 200
rm: OK
tn: ??? 1-81
dt: text
by 288
############
t: 1184
tl: None
ts: 1437376386488
s: true
lb: HTTP??
rc: 200
rm: OK
tn: ??? 1-46
dt: text
by 288
############
t: 964
tl: None
ts: 1437376386708
s: true
lb: HTTP??
rc: 200
rm: OK
tn: ??? 1-8
dt: text
by 288
############
t: 648
tl: None
ts: 1437376387024
s: true
lb: HTTP??
rc: 200
rm: OK
tn: ??? 1-80
dt: text
by 288
############
t: 701
tl: None
ts: 1437376386971
s: true
lb: HTTP??
rc: 200
rm: OK
tn: ??? 1-73
dt: text
by 288
############
t: 983
tl: None
ts: 1437376386689
s: true
lb: HTTP??
rc: 200
rm: OK
tn: ??? 1-16
dt: text
by 288
############
t: 951
tl: None
ts: 1437376386721
s: true
lb: HTTP??
rc: 200
rm: OK
tn: ??? 1-20
dt: text
by 288
############
t: 955
tl: None
ts: 1437376386717
s: true
lb: HTTP??
rc: 200
rm: OK
tn: ??? 1-63
dt: text
by 288
############
find