Python---xml
来源:互联网 发布:网络名词 编辑:程序博客网 时间:2024/05/20 04:48
'''import xml.etree.ElementTree as etreeElementTree is default python module, but it is slow, function is limitedso , we can import etree from lxml, it has the same api.'''from lxml import etree'''import lxml.etree as etreeit has the same function like above'''def getinfo1(path):tree = etree.parse(path)root = tree.getroot()print('========root========')print(root)print(len(root))print('=======tag============')print(root.tag)print('=========child===========')for child in root:print(child)print('======attribute=========')print(root.attrib)print('======attribute1========')print(root[0].attrib)print(root[1].attrib)print('======findall=========')all1=root.findall('{http://www.w3.org/2005/atom}title')print(all1)print(all1[0].find('{http://www.w3.org/2005/atom}des'))print(len(all1))'''findall, return listfind, return the first matched'''print('==========findall2==================')all2=tree.findall('//{http://www.w3.org/2005/atom}*[@href]')print(all2)'''// means all xml file, not just root or its children* means any item[@href] means include href as attribute'''def writeinfo1(path):root = etree.Element('root', lang='en')sub1=etree.SubElement(root, 'title', color='##FFFFFF')sub2= etree.SubElement(root, 'author', color = '##000000')sub2.set('color','##AAAAAA')sub1.set('type','text/html')sub1.text='the content of title will be added'print(etree.tostring(root, pretty_print=True))with open(path, mode='wb') as file:file.write(etree.tostring(root))if __name__ == '__main__':getinfo1('xml_util.xml')writeinfo1('xml_util2.xml')
0 0
- Python & XML
- Python & XML
- python xml
- python xml
- Python XML
- python xml
- python xml
- xml python
- python xml
- Python---xml
- python xml
- python xml
- python 解析XML xml.dom
- python 解析XML xml.dom
- python 解析XML xml.dom
- python解析xml(创建xml)
- python 解析XML xml.dom
- python 解析XML xml.dom
- Python---http
- MVC5+EasyUI企业开发框架系列(01)—总体介绍
- 正则表达式: 以英文字母开头,只能包含英文字母、数字、下划线
- 浅谈 EventBus
- C++输出的精度控制
- Python---xml
- OpenGL基于帧缓存FBO的离屏渲染
- [Leetcode]Serialize and Deserialize Binary Tree
- 技术人如何才能不焦虑
- vc在x64体系的一般传参数方式
- objective-C基础----字典,集合
- iOS——TabelView
- C++本质:类的赋值运算符=的重载,以及深拷贝和浅拷贝
- 关于C中的内存分配问题