学python没多久,遇到一个解析xml的需求,边学边练,用的是ElementTree,感觉没有说的那么方便,也许E文不好,看漏api,root.findall()搞的象正则表达式,瞎了。希望知道的朋友告诉我
题目这样,比如有个xml:
<?xml version="1.0" encoding="UTF-8"?> <students> <student no="2009081097"> <name>Hongten</name> <gender>M</gender> <age>20</age> <score subject="math">97</score> <score subject="chinese">90</score> </student> <student no="2009081098"> <name>DuDu</name> <gender>W</gender> <age>21</age> <score subject="math">87</score> <score subject="chinese">96</score> </student> <student no="2009081099"> <name>Sum</name> <gender>M</gender> <age>19</age> <score subject="math">64</score> <score subject="chinese">98</score> </student> </students>
已知某个值是19,求拥有该值的节点的父节点的所有子节点,就是这么一块:
<name>Sum</name> <gender>M</gender> <age>19</age> <score subject="math">64</score> <score subject="chinese">98</score>
我的代码:
#coding=utf-8
import os
import xml.etree.ElementTree as ET
filepath = 'c:\\test\\a.xml'
tree = ET.parse(filepath)
root = tree.getroot()
p_node = None
r_node = []
def func(node):
global p_node
if len(node)>0:
p_node = node
for c in node:
func(c)
else:
if node.text == '19':#值在这里在这里
r_node.append(p_node)
if __name__ == '__main__':
func(root)
if not r_node:
for n in r_node:
print '-'*10
for c in n:
print c.tag,c.text
else:
print 'not found'
输出:
----------
name Sum
gender M
age 19
score 64
score 98
[Finished in 0.1s]
先这样吧,交差!