zedz01 zedz01 - 1 year ago 199
Python Question

Python XML iterparse() namespacing

According to this post, I successfully can parse my XML file, and reading it's content. However, if I add namespace to it, the whole thing goes wrong.

Let's consider the following XML:

<root xmlns="MyNamespace">

My iterparse looks like this:

context = ET.iterparse('../in/process/teszt.xml', events=('end', ), tag='B1')

I found several examples, but to be honest I don't really understand them, and have no idead how to solve this problem.

Answer Source

In case of XML with default namespace, you need to use the namespace URI along with the element's local name in tag :

context = ET.iterparse('../in/process/teszt.xml', events=('end', ), tag='{MyNamespace}B1')
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download