#coding=UTF8
from xml.etree import ElementTree
xmlText = """
<root>
<body name="lyc">
<age>110</age>
</body>
<body name = "l" age = "10">
</body>
</root>
"""
#xmlText = open("xml.txt").read()
#root = ElementTree.fromstring(xmlText)
#root = ElementTree.parse("xml.txt")
root = ElementTree.fromstring(xmlText)
bodies = root.getiterator("body")
#getiterator
print "getiterator"
print bodies
print dir(bodies[0])
print "attrib:",bodies[0].attrib
print "tag:",bodies[0].tag
print "text",bodies[0].text
#getchildren
print "getchildren"
children = bodies[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
利用Python的xml.etree分析XML
最新推荐文章于 2024-08-06 17:00:20 发布