首页 > 编程语言 > 详细

python xml.etree.ElementTree解析xml文件获取节点

时间:2016-05-18 17:49:59      阅读:174      评论:0      收藏:0      [点我收藏+]

 

<?xml version = "1.0" encoding = "utf-8"?>
<root>
    <body name="lyc">
        <age>110</age>
    </body>
    <body name = "l" age = "10">
    </body>
</root>
 
######################
 
 
#coding=UTF8
 
from xml.etree import ElementTree
 
#xmlText = open("xml.txt").read()
#root = ElementTree.fromstring(xmlText)
 
root = ElementTree.parse("xml.txt")
bodys = root.getiterator("body")
 
#getiterator方法获取
print "getiterator"
print bodys
print dir(bodys[0])
print "attrib:",bodys[0].attrib
print "tag:",bodys[0].tag
print "text",bodys[0].text
#getchildren方法获取
print "getchildren"
children = bodys[0].getchildren()
print children
print "attrib:",children[0].attrib
print "tag:",children[0].tag
print "text:",children[0].text
#find
print "find"
children = root.find("body")
print children
print "attrib:",children.attrib
print "tag:",children.tag
print "text:",children.text
#findall 
print "findall"
children = root.findall("body")
print children
print "attrib:",children[0].attrib
print "tag:",children[0].tag
print "text:",children[0].text
 
 
 
 
 
 
 

aa = xml.dom.minidom.parseString(response_res).documentElement.getElementsByTagName(‘string‘)[0].childNodes[0].data
doc = etree.XML(aa)
doc.xpath("//BoardData")[0].getchildren()[0].text

python xml.etree.ElementTree解析xml文件获取节点

原文:http://www.cnblogs.com/chjbbs/p/5505900.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!