#-*- coding:utf-8 -*-
from import ElementTree
def print_node(node):
''''' prints basic information about the node''''
print "=============================================="
print ":%s" %
if .has_key("age") > 0 :
print "['age']:%s" % ['age']
print ":%s" %
print ":%s" %
def read_xml(text):
''''' read xml file ''''
# Load XML file (2 ways, one is to load the specified string, the other is to load the specified file)
# root = (r"D:\")
root = (text)
# Methods to get element
# 1 via getiterator
lst_node = ("person")
for node in lst_node:
print_node(node)
# 2 by getchildren
lst_node_child = lst_node[0].getchildren()[0]
print_node(lst_node_child)
# 3 .find method
node_find = ('person')
print_node(node_find)
#4. findall method
node_findall = ("person/name")[1]
print_node(node_findall)
if __name__ == '__main__':
# read_xml(open("").read())
write_xml(open("").read())