`
喜欢蓝色的我
  • 浏览: 359919 次
  • 性别: Icon_minigender_2
  • 来自: 北京
社区版块
存档分类
最新评论

ElementTree(元素树)

 
阅读更多

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

分享到:
评论
1 楼 haoningabc 2015-08-11  
           

相关推荐

    element-tree:自定义右键菜单等等

    元素树 一个Vue.js项目 效果 构建设置 # install dependencies npm install # serve with hot reload at localhost:8080 npm run dev # build for production with minification npm run build # build for ...

    HTMLTree:HTML节点树工具箱

    尽管在某些方面类似于ElementTree,但它是专为HTML重新设计的,并不遵循ElementTree的许多XML特定模式。 例如,所有文本均表示为子节点,而不是元素节点的特殊text和tail属性。 这是使用HTML的一种自然得多的方式,...

    Python构建XML树结构的方法示例

    from xml.etree import ElementTree as ET import sys root=ET.Element('color') #用Element类构建标签 root.text=('black') #设置元素内容 tree=ET.ElementTree(root) #创建数对象,参数为根节点对象 tree.write...

    回调函数的意义以及python实现实例

    ElementTree(元素树) ElementTree就像一个轻量级的DOM,具有方便友好的API。代码可用性好,速度快,消耗内存少。 注:因DOM需要将XML数据映射到内存中的树,一是比较慢,二是比较耗内存,而SAX流式读取XML文件,比较...

    java基于链表实现树结构(算法源码)

    //返回当前节点后代元素的数目,即以当前节点为根的子树的规模 public int getSize() { int size = 1;//当前节点也是自己的后代 TreeLinkedList subtree = firstChild;//从长子开始 while (null != subtree) ...

    AVLTree:具有以下操作的通用AVL树实现:插入,删除,搜索,上下限,最近的元素,范围内的值等

    问题:AVL树目的:了解平衡二叉搜索树的端到端知识,以及如何将其有效地用于解决各种问题。 任务:通过以下操作实现AVL树。...9. Count the number of elements in the tree whose values fall into a given range.

    elementx:从功能上创建DOM元素并将它们快速组合为树

    :high_voltage:使用功能性方法创建复杂的元素/树。 该模块为那些希望使用纯函数组合构建DOM树的用户提供了或的替代方法。 const { div , h1 , h2 , button , ul , li } = require ( 'elementx' )div ( h1 ( { class ...

    html5lib-python:符合标准的库,用于在Python中解析和序列化HTML文档和片段

    用法 简单用法遵循以下模式: ... 或者: ... 默认情况下, document将是xml.etree... html5lib尽可能选择加速的ElementTree实现(即Python 2.x上的xml.etree.cElementTree )。 支持其他两种树类型: xml.dom.minidom和lx

    node-asn1-tree

    嵌套元素形成一棵树,因此名称为“ asn1-tree”,而原始值将仅生成一个对象。 这只是一个编码器/解码器。 结构的解释(即与模式匹配)超出了该库的范围。 ,请参阅ASN.1模式映射器 。 安装 npm install --save ...

    XML轻松学习手册--XML肯定是未来的发展趋势,不论是网页设计师还是网络程序员,都应该及时学习和了解

    一个文档结构树包含根元素,根元素是最顶级的元素,(就是紧接着XML声明语句后的第一个元素)。看例子: &lt;filelist&gt; &lt;title&gt;... &lt;author&gt;... 上面的例子分三级结构排列成"树"状,其中的就是根元素。在XML...

    js_tree-from-object-DOM

    运行npm run test:only -- -n以忽略linter的方式...来自对象的树编写一个函数createTree(element, data) ,该函数从一个对象创建ul/li的嵌套列表。 使用键作为列表项。 element -是DOM元素data -是嵌套对象提示:使用

    C#数据结构

    (3) 树形结构(Tree Structure):如图1.1(c)所示,该结构中的数据元素存在着一对 多的关系。 (4) 图状结构(Graphic Structure):如图1.1(d)所示,该结构中的数据元素存在着 多对多的关系。 (a) 集合 (b) 线性结构 (c)...

    STL 源码剖析(侯捷先生译著)

    5.1.2 平衡二元搜寻树(balanced binary search tree) 203 5.1.3 AVL tree(Adelson-Velskii-Landis tree) 203 5.1.4 单旋转(Single Rotation) 205 5.1.5 双旋转(Double Rotation) 206 5.2 RB-tree(红黑...

    STL源码剖析.pdg

    5.1.2 平衡二元搜寻树(balanced binary search tree) 203 5.1.3 avl tree(adelson-velskii-landis tree) 203 5.1.4 单旋转(single rotation) 205 5.1.5 双旋转(double rotation) 206 5.2 rb-tree(红黑...

    Ext Js权威指南(.zip.001

    7.5.11 树节点:ext.data.nodeinterface与ext.data.tree / 364 7.5.12 store的方法 / 366 7.5.13 store的事件 / 368 7.5.14 store管理器:ext.data.storemanager / 369 7.6 综合实例 / 369 7.6.1 远程读取json...

    JavaScript数据结构学习之数组、栈与队列

    数组,队列(queue),堆(heap),栈(stack),链表(linked list ),树(tree),图(graph)和散列表(hash) 本文主要介绍的是数组、栈与队列,下面来一起看看详细的介绍吧。 一、数组 数组是平时使用最常用的...

Global site tag (gtag.js) - Google Analytics