In python, what is the best way to extract the list of items from the following xml?
<iq xmlns="jabber:client" to="__anonymous__admin@localhost/8978528613056092673206"
from="conference.localhost" id="disco" type="result">
<query xmlns="http://jabber.org/protocol/disco#items">
<item jid="pgatt@conference.localhost" name="pgatt (1)"/>
<item jid="pgatt@conference.localhost" name="pgatt (1)"/>
</query>
</iq>
I usually use lxml with xpath, but it’s not working in this case. I think my problems are due to namespaces. I’m not set on lxml and am open to using any library.
I would like a solution that is robust enough to fail if the general structure of the xml changes.
I’m not sure about
lxmlbut you can use an expression like//*[local-name()="item"]to pull out theitemelements regardless of their namespace.You might also want to take a look at Amara for XML processing.
Once I discovered Amara, I would never consider processing XML any other way.