Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4613468
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T01:33:18+00:00 2026-05-22T01:33:18+00:00

In python, what is the best way to extract the list of items from

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T01:33:19+00:00Added an answer on May 22, 2026 at 1:33 am

    I’m not sure about lxml but you can use an expression like //*[local-name()="item"] to pull out the item elements regardless of their namespace.

    You might also want to take a look at Amara for 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>''')
    >>> for item in doc.iq.query.item:
    ...   print item.jid, item.name
    ...
    pgatt@conference.localhost pgatt (1)
    pgatt@conference.localhost pgatt (1)
    >>>
    

    Once I discovered Amara, I would never consider processing XML any other way.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

What is the best way to sniff network packets using Python? I've heard from
What is the best way of creating an alphabetically sorted list in Python?
What is the best way to create a new empty list in Python? l
What is the best way to do the following in Python: for item in
What is the best way to loop over a python string backwards? The following
I found this thread: Best way to strip punctuation from a string in Python
What's the best way to extract keyphrases from a block of text? I'm writing
What is the best way to pass List via form in python (cgi). ListStr
Possible Duplicate: Extracting text from HTML file using Python What is the best way
What would be the best way in Python to parse out chunks of text

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.