ElementTree处理XML的相关知识
2016-02-01 09:52
316 查看
<span style="font-family: SimHei; background-color: rgb(255, 255, 255);">一、导入</span>
try:
11、tree.write('path')以tree建立XML文档。
在这里,elem.clear()是关键,iterparse()依然会建立一棵树,只是不用全部加载进内存。
try:
<span style="font-family:FangSong_GB2312;font-size:14px;"> import xml.etree.cElementTree as ET except ImportError: import xml.etree.ElementTree as ET</span>
<span style="font-family:FangSong_GB2312;font-size:14px;"> ElementTree在python标准库中有两种实现,一种是纯python的ElementTree,另一种是用C语言实现的cElementTree,速度较快,占用内存也小,推荐使用。</span>
<span style="font-family:FangSong_GB2312;font-size:14px;"><span style="font-family: SimHei; font-size: 18px; "><strong>二、相关知识点介绍</strong></span></span>
<span style="font-family:FangSong_GB2312;font-size:14px;">1、<code class="docutils literal" style="color: rgb(34, 34, 34); line-height: 24px;"><span class="pre">ElementTree</span></code><span style="color: rgb(62, 67, 73); line-height: 24px;"> 将整个 XML 解析为一棵树, </span><code class="docutils literal" style="color: rgb(34, 34, 34); line-height: 24px;"><span class="pre">Element</span></code><span style="color: rgb(62, 67, 73); line-height: 24px;"> 将单个结点解析为树。</span></span>
<span style="font-family:FangSong_GB2312;font-size:14px;"><span style="color: rgb(62, 67, 73); line-height: 24px;">2、整个文档级别的操作通常用 </span><code class="docutils literal" style="color: rgb(34, 34, 34); line-height: 24px;"><span class="pre">ElementTree</span></code><span style="color: rgb(62, 67, 73); line-height: 24px;"> 。单个 XML 元素和它的子元素通常用 </span><code class="docutils literal" style="color: rgb(34, 34, 34); line-height: 24px;"><span class="pre">Element</span></code><span style="color: rgb(62, 67, 73); line-height: 24px;"> 。</span></span>
<span style="line-height: 24px;"></span><pre name="code" class="python" style="font-size: 16px; color: rgb(62, 67, 73); font-family: Nobile, sans-serif;"><span style="font-family:FangSong_GB2312;font-size:14px;"><span style="font-family: SimHei; font-size: 18px; "><strong>三、常用函数和方法</strong></span></span>
<pre name="code" class="python" style="font-size: 16px;"><span style="color: rgb(62, 67, 73); font-family: FangSong_GB2312; font-size: 14px;">1、</span><span style="font-size: 14px;"><span style="color:#222222;">tree = ET.ElementTree(file = "XPath")</span></span>
<span style="color:#222222;"><span style="font-size: 14px;">该函数参数是XML文件的路径,返回tree。 </span></span>
<span style="color:#222222;"><span style="font-size: 14px;">2、root = tree.getroot()</span></span>
<span style="color:#222222;"><span style="font-size: 14px;">返回根节点,是一个Element对象。可以通过root.tag, root.attrib访问根节点的标签和属性。root可以用于循环遍历,来查找root的子节点。</span></span>
<span style="color:#222222;"></span><pre name="code" class="python"><span style="font-family:FangSong_GB2312;font-size:14px;">for child_node_root in root: print child_node_root.tag, child_node_root.attrib</span>返回的tag是一个字符串,attrib是一个字典。
<span style="color:#222222;"><span style="font-size: 14px;">也可以通过索引的方式访问root的子节点。</span></span>
<span style="color:#222222;"><span style="font-size: 14px;">root[0].tag, root[0].attrib, root[0].test。</span></span>
<span style="color:#222222;"><span style="font-size: 14px;">3、</span></span><span style="font-size: 14px; color: rgb(34, 34, 34); font-family: Arial, Helvetica, sans-serif;">for elem in tree.iter():</span><span style="color: rgb(34, 34, 34);"></span><pre name="code" class="python"><pre name="code" class="python" style="font-size: 14px;"> print elem.tag, elem.attrib
Element和ElementTree的对象有一个iter()方法,可以深度遍历子节点。iter()方法也可以接受一个标签名作为参数,只遍历含有指定标签名的子节点。
4、find()、findall()、iterfind() 这3个方法对Element和ElementTree对象都适用,find()返回第一个匹配的子元素,findall()返回所有匹配的子元素,iterfind()为所有匹配项提供迭代器。
<pre style="font-size: 1.1em; overflow-x: auto; overflow-y: hidden; padding: 10px; background-color: rgb(250, 250, 250); color: rgb(34, 34, 34); line-height: 1.2em; border-width: 2px; border-style: solid none; border-color: rgb(198, 201, 203); margin-top: 1.5em; margin-bottom: 1.5em;"><span class="k" style="color: rgb(0, 112, 32); font-weight: bold;">for</span> <span class="n">elem</span> <span class="ow" style="color: rgb(0, 112, 32); font-weight: bold;">in</span> <span class="n">tree</span><span class="o" style="color: rgb(102, 102, 102);">.</span><span class="n">iterfind</span><span class="p">(</span><span class="s" style="color: rgb(64, 112, 160);">'branch/sub-branch'</span><span class="p">):</span> <span class="gp" style="color: rgb(198, 93, 9); font-weight: bold;">... </span> <span class="k" style="color: rgb(0, 112, 32); font-weight: bold;">print</span> <span class="n">elem</span><span class="o" style="color: rgb(102, 102, 102);">.</span><span class="n">tag</span><span class="p">,</span> <span class="n">elem</span><span class="o" style="color: rgb(102, 102, 102);">.</span><span class="n">attrib</span> <span class="gp" style="color: rgb(198, 93, 9); font-weight: bold;">...</span> <span class="go" style="color: rgb(51, 51, 51);">sub-branch {'name': 'subrelease01'}</span>
<pre name="code" class="python"><span style="color: rgb(34, 34, 34);"><span style="font-size: 14px;"></span></span><pre name="code" class="python">5、del root[2]删除某个节点
6、root[0].set("attrib_name", "attrib"),为子节点添加或修改属性。
7、a = ET.Element('elem')创建一个名为elem的标签
8、b = ET.SubElement(a, 'sub_elem')创建一个子节点,父节点是a,子节点标签名是sub_elem。
9、root = ET.Element('root'), root.extend((a)),将a节点添加到root节点下。
10、tree = ET.ElementTree(root),以root为根节点建立的XML树,
11、tree.write('path')以tree建立XML文档。
<span style="font-size: 14px;">四、一些补充</span>
<span style="font-size: 14px;">当XML文档较大时,全部读入内存会消耗过多的系统资源,可以利用iterparse()和clear()方法。</span>
<span style="font-size: 14px;"></span><pre name="code" class="python"><span style="color: rgb(34, 34, 34);"><span style="font-size: 14px;"></span></span>
count = 0 for event, elem in ET.iterparse(sys.argv[2]): if event == 'end': if elem.tag == 'location' and elem.text == 'Zimbabwe': count += 1 elem.clear() # discard the element print count
在这里,elem.clear()是关键,iterparse()依然会建立一棵树,只是不用全部加载进内存。
<span style="color: rgb(34, 34, 34);"><span style="font-size: 14px;"></span></span><pre name="code" class="python">for elem in tree.iter(): print elem, elem.tag, elem.attrib
相关文章推荐
- 深入分析JavaWeb 2 -- Tomcat服务器学习和使用
- oracle - redo 损坏或删除处理方法
- SQL 跟踪方法相关介绍
- Handlebars的使用
- C语言实现双向循环链表
- 收缩 表空间
- oracle 配置 oem
- 索引大小及占表的空间
- codeforces621B. Wet Shark and Bishops
- 构建高并发高可用的电商平台架构实践
- Oracle 11g Windows 迁移至 Linux
- ORACLE 单实例完全卸载数据库
- 出现( linker command failed with exit code 1)错误总结
- 用python调用C/C++的两种方式
- io资料
- RMAN - 备份异机恢复
- 登陆注册的邮箱格式判断
- Vfork
- RMAN - "丢失控制文件的恢复"
- SQL开发 循序渐进