python 访问xml节点方法

import xml.etree.ElementTree as ET
 cur_xml = ET.parse(annotation_path+file)
 subroot = cur_xml.getroot().getchildren()
关键掌握两个方法既可以很顺溜的访问xml的节点一个是
.getroot()
另外一个是
.getchildren()
还有的节点的标签node.tag就可以访问,当然你也可以使用下标index,下标访问有bug,就是顺序一定要提前固定好对于节点顺序打乱的话就会变得很难维护,因此建议使用
.getchildren()
得到孩子节点之后使用循环遍历对节点的标签进行访问,再结合着index就可以实现完美的访问。






猜你喜欢

转载自blog.csdn.net/touch_dream/article/details/80705277