python类库[使用xml.etree.ElementTree读写xml]
2014-03-23 23:44
591 查看
一 基本知识
1、插入节点
Element.insert(index, element) 、Element(tag[, attrib][, **extra]) 、SubElement(parent,
tag[, attrib[, **extra]]) 、Element.append(subelement)
2、删除节点
Element.remove(subelement) 删除一个节点、Element.clear()删除该节点下所有子节点
3、在节点中插入属性
Element.set(key, value)
4、查找节点
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>
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())
参考:
http://blog.csdn.net/kiki113/archive/2009/04/06/4052584.aspx
完!
一 基本知识
1、插入节点
Element.insert(index, element) 、Element(tag[, attrib][, **extra]) 、SubElement(parent,
tag[, attrib[, **extra]]) 、Element.append(subelement)
2、删除节点
Element.remove(subelement) 删除一个节点、Element.clear()删除该节点下所有子节点
3、在节点中插入属性
Element.set(key, value)
4、查找节点
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>
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())
参考:
http://blog.csdn.net/kiki113/archive/2009/04/06/4052584.aspx
完!
相关文章推荐
- python类库31[使用xml.etree.ElementTree读写xml]
- python类库31[使用xml.etree.ElementTree读写xml]
- 使用Python和xml.etree.ElementTree解析xml文件
- python 6-4 如何构建xml文档使用标准库中的xml.etree.ElementTree 构建ElementTree,使用write方法写入文档
- python3.2 读写 xml( xml.etree.ElementTree,xml.dom )
- [ZT]Python使用xml.etree.ElementTree操作xml文件
- python 6-3 如何解析简单的xml文档 使用标准库中的xml.etree.ElementTree,其中parse函数可以解析xml文档
- python xml包 xml.etree.ElementTree使用记录
- Python xml.etree.ElementTree 用法
- Python xml属性/节点/文本的增删改[xml.etree.ElementTree]
- python模块:xml.etree.ElementTree
- python类库31[使用minidom读写xml]
- python模块之xml.etree.ElementTree
- python xml.etree.ElementTree解析xml文件获取节点
- python类库31[使用minidom读写xml]
- 使用 Python ElementTree 生成 xml
- python类库31[使用minidom读写xml]
- Python xml.etree.ElementTree解析XML文件实例演示(十六02)
- python模块xml,xml.etree.ElementTree 节点的读取,查询,修改,保存,创建,删除等
- 使用xml.etree.ElementTree读xml