Parsing XML with namespace in Python via 'ElementTree'

前端 未结 6 1684
臣服心动
臣服心动 2020-11-21 09:48

I have the following XML which I want to parse using Python\'s ElementTree:



        
6条回答
  •  梦如初夏
    2020-11-21 10:21

    I've been using similar code to this and have found it's always worth reading the documentation... as usual!

    findall() will only find elements which are direct children of the current tag. So, not really ALL.

    It might be worth your while trying to get your code working with the following, especially if you're dealing with big and complex xml files so that that sub-sub-elements (etc.) are also included. If you know yourself where elements are in your xml, then I suppose it'll be fine! Just thought this was worth remembering.

    root.iter()
    

    ref: https://docs.python.org/3/library/xml.etree.elementtree.html#finding-interesting-elements "Element.findall() finds only elements with a tag which are direct children of the current element. Element.find() finds the first child with a particular tag, and Element.text accesses the element’s text content. Element.get() accesses the element’s attributes:"

提交回复
热议问题