Find all nodes by attribute in XML using Python 2

后端 未结 2 855
佛祖请我去吃肉
佛祖请我去吃肉 2021-01-13 20:26

I have an XML file which has a lot of different nodes with the same attribute.

I was wondering if it\'s possible to find all these nodes using Python and any additi

2条回答
  •  清歌不尽
    2021-01-13 20:42

    This is a good sample/start script using xpath :

    # -*- coding: utf-8 -*-
    from lxml import etree
    fp = open("xml.xml")
    tree = etree.parse(fp)
    for el in tree.findall('//node[@attr="something"]'):
        print(el.text)
    

提交回复
热议问题