Search code examples
pythonxmlelementtreecelementtree

Python: Using namespaces in ElementTree with iter()


I am using xml.etree.ElementTree to parse some complex xml files. Some of the xml files have several repeating tags nested in them.

<product:object>
  <product:parent>
    <product:parent>
      <product:parent>
      </product:parent>
    </product:parent>
  </product:parent>
</product:object>

I am using .iter() to find the repeating tag in different layers. Normally a second argument can be passed to .find() and .findall(). However, for some reason .iter() doesn't have this option.

Am I missing something, or is there another way of properly doing this?

I know how to, and have build workarounds. e.g. - A definition that is reiterated and passes the parent element. - Manually mapping the namespaces

I am hoping there is a better way!?


Solution

  • I found that using XPath syntax .findall(.//product:parent, ns) can be used as a substitute for .iter()