python類庫31[使用xml.etree.ElementTree讀寫xml]
一 基本知識
1、插入節(jié)點
Element.insert(index, element) 、Element(tag[, attrib][, **extra]) 、SubElement(parent, tag[, attrib[, **extra]]) 、Element.append(subelement)
2、刪除節(jié)點
Element.remove(subelement) 刪除一個節(jié)點、Element.clear()刪除該節(jié)點下所有子節(jié)點
3、在節(jié)點中插入屬性
Element.set(key, value)
4、查找節(jié)點
a) Element.getiterator b) Element.getchildren c) Element.find d) Element.findall
二 讀取xml
1)xml為
<?xml version="1.0" encoding="UTF-8"?>
<employees>
<employee id = '1'>
<name>linux</name>
<age>30</age>
</employee>
<employee id = '2'>
<name>windows</name>
<age>20</age>
</employee>
</employees>
<employees>
<employee id = '1'>
<name>linux</name>
<age>30</age>
</employee>
<employee id = '2'>
<name>windows</name>
<age>20</age>
</employee>
</employees>
2)python腳本為
from xml.etree import ElementTree
def print_node(node):
print "====================================="
for key,value in node.items():
print "%s:%s" % (key, value)
for subnode in node.getchildren():
print "%s:%s" % (subnode.tag, subnode.text)
def read_xml(text = '', xmlfile = ''):
#root = ElementTree.parse(xmlfile)
root = ElementTree.fromstring(text)
# 1 getiterator([tag=None])
# only elements whose tag equals tag are returned from the iterator
eitor = root.getiterator("employee")
for e in eitor:
print_node(e)
# 2 getchildren()
# Returns all subelements
eitor = root.getchildren()
for e in eitor:
print_node(e)
# 3 findall(match)
# Finds all subelements matching match.
# match may be a tag name or path. Returns an iterable yielding all matching elements
node_findall = root.findall("employee")
for e in node_findall:
print_node(e)
# 4 find(match)
# Finds the first subelement matching match.
# match may be a tag name or path. Returns an element instance or None
node_find = root.find('employee')
print_node(node_find)
if __name__ == '__main__':
read_xml(open("employees.xml").read())
def print_node(node):
print "====================================="
for key,value in node.items():
print "%s:%s" % (key, value)
for subnode in node.getchildren():
print "%s:%s" % (subnode.tag, subnode.text)
def read_xml(text = '', xmlfile = ''):
#root = ElementTree.parse(xmlfile)
root = ElementTree.fromstring(text)
# 1 getiterator([tag=None])
# only elements whose tag equals tag are returned from the iterator
eitor = root.getiterator("employee")
for e in eitor:
print_node(e)
# 2 getchildren()
# Returns all subelements
eitor = root.getchildren()
for e in eitor:
print_node(e)
# 3 findall(match)
# Finds all subelements matching match.
# match may be a tag name or path. Returns an iterable yielding all matching elements
node_findall = root.findall("employee")
for e in node_findall:
print_node(e)
# 4 find(match)
# Finds the first subelement matching match.
# match may be a tag name or path. Returns an element instance or None
node_find = root.find('employee')
print_node(node_find)
if __name__ == '__main__':
read_xml(open("employees.xml").read())
參考:
http://blog.csdn.net/kiki113/archive/2009/04/06/4052584.aspx
完!


浙公網(wǎng)安備 33010602011771號