Is there an elegant way to count tag elements in a xml file using lxml in python?

前端 未结 3 2110
暗喜
暗喜 2021-02-13 01:35

I could read the content of the xml file to a string and use string operations to achieve this, but I guess there is a more elegant way to do this. Since I did not find a clue i

3条回答
  •  臣服心动
    2021-02-13 02:23

    If you want to count all author tags:

    import lxml.etree
    doc = lxml.etree.parse(xml)
    count = doc.xpath('count(//author)')
    

提交回复
热议问题