转载:https://www.cnblogs.com/hongten/p/hongten_python_xml_etree_elementtree.html

 1 # -*- coding: utf-8 -*-
2 #python xml.etree.ElementTree
3
4 #Author : Hongten
5 #Mailto : hongtenzone@foxmail.com
6 #Blog : http://www.cnblogs.com/hongten
7 #QQ : 648719819
8 #Version : 1.0
9 #Create : 2013-09-03
10
11 import os
12 import xml.etree.ElementTree as ET
13
14 '''
15 在python中,解析XML文件有很多中方法
16 本文中要使用的方法是:xml.etree.ElementTree
17 '''
18 #global var
19 #show log
20 SHOW_LOG = True
21 #XML file
22 XML_PATH = None
23
24 def get_root(path):
25 '''parse the XML file,and get the tree of the XML file
26 finally,return the root element of the tree.
27 if the XML file dose not exist,then print the information'''
28 if os.path.exists(path):
29 if SHOW_LOG:
30 print('start to parse the file : [{}]'.format(path))
31 tree = ET.parse(path)
32 return tree.getroot()
33 else:
34 print('the path [{}] dose not exist!'.format(path))
35
36 def get_element_tag(element):
37 '''return the element tag if the element is not None.'''
38 if element is not None:
39 if SHOW_LOG:
40 print('begin to handle the element : [{}]'.format(element))
41 return element.tag
42 else:
43 print('the element is None!')
44
45 def get_element_attrib(element):
46 '''return the element attrib if the element is not None.'''
47 if element is not None:
48 if SHOW_LOG:
49 print('begin to handle the element : [{}]'.format(element))
50 return element.attrib
51 else:
52 print('the element is None!')
53
54 def get_element_text(element):
55 '''return the text of the element.'''
56 if element is not None:
57 return element.text
58 else:
59 print('the element is None!')
60
61 def get_element_children(element):
62 '''return the element children if the element is not None.'''
63 if element is not None:
64 if SHOW_LOG:
65 print('begin to handle the element : [{}]'.format(element))
66 return [c for c in element]
67 else:
68 print('the element is None!')
69
70 def get_elements_tag(elements):
71 '''return the list of tags of element's tag'''
72 if elements is not None:
73 tags = []
74 for e in elements:
75 tags.append(e.tag)
76 return tags
77 else:
78 print('the elements is None!')
79
80 def get_elements_attrib(elements):
81 '''return the list of attribs of element's attrib'''
82 if elements is not None:
83 attribs = []
84 for a in elements:
85 attribs.append(a.attrib)
86 return attribs
87 else:
88 print('the elements is None!')
89
90 def get_elements_text(elements):
91 '''return the dict of element'''
92 if elements is not None:
93 text = []
94 for t in elements:
95 text.append(t.text)
96 return dict(zip(get_elements_tag(elements), text))
97 else:
98 print('the elements is None!')
99
100 def init():
101 global SHOW_LOG
102 SHOW_LOG = True
103 global XML_PATH
104 XML_PATH = 'c:\\test\\hongten.xml'
105
106 def main():
107 init()
108 #root
109 root = get_root(XML_PATH)
110 root_tag = get_element_tag(root)
111 print(root_tag)
112 root_attrib = get_element_attrib(root)
113 print(root_attrib)
114 #children
115 children = get_element_children(root)
116 print(children)
117 children_tags = get_elements_tag(children)
118 print(children_tags)
119 children_attribs = get_elements_attrib(children)
120 print(children_attribs)
121
122 print('#' * 50)
123 #获取二级元素的每一个子节点的名称和值
124 for c in children:
125 c_children = get_element_children(c)
126 dict_text = get_elements_text(c_children)
127 print(dict_text)
128
129 if __name__ == '__main__':
130 main()

xml.etree.ElementTree模块的封装的更多相关文章

  1. python xml.etree.ElementTree模块

    使用的XML文件如下:file.xml <?xml version="1.0"?> <data name="ming"> <cou ...

  2. [python 学习] 使用 xml.etree.ElementTree 模块处理 XML

    ---恢复内容开始--- 导入数据(读文件和读字符串) 本地文件 country_data.xml <?xml version="1.0"?> <data> ...

  3. python 之xml.etree.ElementTree

    Element类型是一种灵活的容器对象,用于在内存中存储结构化数据. [注意]xml.etree.ElementTree模块在应对恶意结构数据时显得并不安全. 每个element对象都具有以下属性: ...

  4. Python 标准库之 xml.etree.ElementTree

    Python 标准库之 xml.etree.ElementTree Python中有多种xml处理API,常用的有xml.dom.*模块.xml.sax.*模块.xml.parser.expat模块和 ...

  5. python模块:xml.etree.ElementTree

    """Lightweight XML support for Python. XML is an inherently hierarchical data format, ...

  6. python模块之xml.etree.ElementTree

    xml.etree.ElementTree用于解析和构建XML文件 <?xml version="1.0"?> <data> <country nam ...

  7. python xml包 xml.etree.ElementTree使用记录

    19.7.1 教程 这是一个简短的教程使用xml.etree.ElementTree(简称为et).目标是展示一些构建模块和模块的基本概念 9.7.1.1. XML tree and elements ...

  8. [xml]AttributeError: 'xml.etree.ElementTree.Element' object has no attribute 'getroot'

    >>> import requests >>> res = requests.get("https://xxx.com/sitemap.xml" ...

  9. xml.etree.ElementTree对CDATA的输出

    xml.etree.ElmentTree不支持CDATA 的输出,但是支持Comment的输出.由于在项目中需要输出带有CDATA块的XML文本,参考Comment的做法,修改ElmentTree中的 ...

随机推荐

  1. IOS 滑动指示导航栏 渐变

    关于导航栏渐变,本人在写APP的时候,发现了各路大神各现其通,其实我觉得这个是个很简单的问题,不需要搞得那么麻烦,对个项目要求整齐来说,一般会建一个工具类,自定义View,各个同事需要的时候,直接调用 ...

  2. 【git】把本地项目和远程git仓库相连通

    1. 打开在你的项目文件夹,输入下面的命令 git init 输完上面的命令,文件夹中会出现一个.git文件夹,如下图所示,其他的的文件也会出现蓝色小问号的标志 2. 添加所有文件 git add . ...

  3. [翻译]现代Linux系统上的栈溢出攻击【转】

    转自:http://www.codeweblog.com/%E7%BF%BB%E8%AF%91-%E7%8E%B0%E4%BB%A3linux%E7%B3%BB%E7%BB%9F%E4%B8%8A%E ...

  4. 什麼是 usb upstream port

    主機USB埠是定義為USB纜線的上行端(Upstream)或「A」接頭,即PC端.而裝置USB埠是定義為USB纜線的下行端(Downstream)或「B」接頭,即行動產品端. Reference ht ...

  5. linux创建和查看用户命令

    1.创建一个叫做hadoop的用户,用户的目录是/home/hadoop useradd -d /home/hadoop hadoop 2.输入密码 passwd hadoop 3.删除用户 user ...

  6. LeetCode OJ-- Binary Tree Maximum Path Sum ***

    https://oj.leetcode.com/problems/binary-tree-maximum-path-sum/ 给一棵二叉树,路径可以从任一点起,到任一点结束,但是可以连成一个路径的.求 ...

  7. 使用Nginx+FFMPEG搭建HLS直播转码服务器

    目的:使Nginx支持Rtmp协议推流,并支持hls分发功能及FFMPEG转码多码率功能. 一.准备工作 模块:nginx-rtmp-module-master(支持rtmp协议) 下载地址: htt ...

  8. 【原创】打开Excel时提示"您尝试打开的文件**.xls的格式与文件扩展名指定的格式不一致"

    问题描述:     系统安装了WPS时,Analyzer导出excel时候,会提示"您尝试打开的文件**.xls的格式与文件扩展名指定的格式不一致",这是Excel的安全问题,   ...

  9. Codeforces Gym101522 D.Distribution of Days-算日期 (La Salle-Pui Ching Programming Challenge 培正喇沙編程挑戰賽 2017)

    D.Distribution of Days The Gregorian calendar is internationally the most widely used civil calendar ...

  10. MapReduce编程模型及其在Hadoop上的实现

    转自:https://www.zybuluo.com/frank-shaw/note/206604 MapReduce基本过程 关于MapReduce中数据流的传输过程,下图是一个经典演示:  关于上 ...