Extract items list from XML in python -


in python, best way extract list of items 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 use lxml xpath, it's not working in case. think problems due namespaces. i'm not set on lxml , open using library.

i solution robust enough fail if general structure of xml changes.

i'm not sure lxml can use expression //*[local-name()="item"] pull out item elements regardless of namespace.

you might want take @ amara xml processing.

>>> import amara.bindery >>> doc = amara.bindery.parse( ...     '''<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>''') >>> item in doc.iq.query.item: ...   print item.jid, item.name ... pgatt@conference.localhost pgatt (1) pgatt@conference.localhost pgatt (1) >>> 

once discovered amara, never consider processing xml other way.


Comments

Popular posts from this blog

c# - how to write client side events functions for the combobox items -

exception - Python, pyPdf OCR error: pyPdf.utils.PdfReadError: EOF marker not found -