python解析XML方法
2015-12-04 00:00
411 查看
python有三种方法解析XML,SAX,DOM,以及ElementTree不止各有利弊比如xml.dom.minidom方法。
权衡自己学习ElementTree和xml.dom.minidom。
XML语法看w3c,主要看xml的层数和复杂程度,数据量大小。
一:根据看的内容总结xml.dom.minidom方法简单应用:
主要用到 xml.dom.minidom中的parse和documentElement,xml.dom.minidom.parse('file.xml')打开,xml.dom.minidom.documentElement读xml
就是有一个abc.xml文件后,
import xml.dom.minidom
dom = xml.dom.minidom.parse('abc.xml')#打开xml文档
root = dom.documentElement#读取xml
使用技巧:
root.getElementsByTagName('caption') 获得的是标签为caption 一组标签,b[0]表示一组标签中的第一个;b[2] ,表示这一组标签中的第三个。
getAttribute方法可以获得元素的属性所对应的值。
firstChild 属性返回被选节点的第一个子节点,.data表示获取该节点人数据。
二:.ElementTree(元素树)方法:
与上面的xml.dom.minidom比较而言更利于对复杂的xml进行遍历查找,而且他是轻量级,速度快,内存消耗小。
demo:
以上分别从不同帖子整理。
权衡自己学习ElementTree和xml.dom.minidom。
XML语法看w3c,主要看xml的层数和复杂程度,数据量大小。
一:根据看的内容总结xml.dom.minidom方法简单应用:
主要用到 xml.dom.minidom中的parse和documentElement,xml.dom.minidom.parse('file.xml')打开,xml.dom.minidom.documentElement读xml
就是有一个abc.xml文件后,
import xml.dom.minidom
dom = xml.dom.minidom.parse('abc.xml')#打开xml文档
root = dom.documentElement#读取xml
使用技巧:
root.getElementsByTagName('caption') 获得的是标签为caption 一组标签,b[0]表示一组标签中的第一个;b[2] ,表示这一组标签中的第三个。
getAttribute方法可以获得元素的属性所对应的值。
firstChild 属性返回被选节点的第一个子节点,.data表示获取该节点人数据。
二:.ElementTree(元素树)方法:
与上面的xml.dom.minidom比较而言更利于对复杂的xml进行遍历查找,而且他是轻量级,速度快,内存消耗小。
demo:
<?xml version="1.0" encoding="utf-8"?> <root> <person age="27"> <name>tianma</name> <password>manager</password> </person> <person age="28" des="hello"> <name>xingkong</name> <password>admin</password> </person> </root>
#coding=utf-8 from xml.etree import ElementTree as ET def print_node(node): '''print node message''' print "==============================================" print "node.attrib:%s" % node.attrib if node.attrib.has_key("age") > 0: print "node.attrib['age']:%s" % node.attrib['age'] print "node.tag:%s" % node.tag print "node.tag:%s" % node.text def read_xml(text): '''read xml file''' # root = ET.parse(r"C:\Users\yshe\Desktop\testxml") root = ET.fromstring(text) #1 Go through getiterator method lst_node = root.getiterator("person") for node in lst_node: print_node(node) #2 Go througn getchildren method get subarea node &nb 7fe0 sp; lst_node_child = lst_node[0].getchildren()[0] print_node(lst_node_child) #3 Go througn find method node_find = root.find('person') print_node(node_find) #4 Go through findall method node_findall = root.findall("person/name")[1] print_node(node_findall) if __name__ == '__main__': read_xml(open("test.xml").read())
#coding=utf-8 from xml.etree import ElementTree as ET #Find out all persons age per=ET.parse('test.xml') p=per.findall('person') for x in p: print x.attrib print for oneper in p: #Find out person node for child in oneper.getchildren(): #Find out person child node print child.tag,':',child.text print 'age:',oneper.get('age') print "=============================================="
以上分别从不同帖子整理。
相关文章推荐
- Conque ERROR: Python interface cannot be loaded
- Python.h: No such file or directory
- Python结构化编程
- python requests 高级用法 -- 包括SSL 证书错误的解决方案
- python execl转xml应用
- 使用Python开发windows GUI程序入门实例
- Python脚本自动化编译RPM包
- Python之路(二)
- 17.2. socket — Low-level networking interface Python
- python 中 类, 模块, 包的组织
- 趣学Python-教孩子学编程--第十二章
- 利用python中的pandas,sklearn进行数据挖掘 basic_of_datamining
- Python扩展库psutil用法精要
- Python学习笔记一
- 《python爬虫》学习笔记:urllib2库的使用
- Spark编程指南(Python版)
- LeetCode解题报告--Next Permutation
- 《Python程序设计》在亚马逊京东当当互动出版网淘宝全面上架
- (python)下载秒拍美拍视频
- 《Python程序设计基础》在亚马逊当当京东互动出版网淘宝等各大书店上架