python 解析XML

来源:互联网 发布:杭州淘宝大学电话 编辑:程序博客网 时间:2024/06/05 04:18

xml 格式如上.

首先要得到root节点:

上面的root是class Element的object, 而tree 是ElementTree的object. 如果想得到某个节点的attribute, 例如<node name="name1"> 则element.attrib['name']即可.如果是<edge>text</edge>,就是element.text.

如果想得到下一层节点,可以使用root.find(tag)得到,如果不知道具体的tag,那么可以使用root.getchildren().如果想使用某个tag作为迭代,例如nodes中有两个node.代码如下:

 

原创粉丝点击