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

  • Home
  • SEARCH
  • 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 4250186
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T04:29:43+00:00 2026-05-21T04:29:43+00:00

Say i have this sample XML. <result> <field k=’field1′> <value h=’1′><text>text_value1</text></value> </field> <field k=’field2′>

  • 0

Say i have this sample XML.

<result>
    <field k='field1'>
        <value h='1'><text>text_value1</text></value>
    </field>
    <field k='field2'>
        <value><text>text_value2</text></value>
    </field>
    <field k='field3'>
        <value><text>some_text</text></value>
    </field>
</result>

Using python’s lxml, how can i get the value of each field for every result set? So basically, i want to iterate over ever result set, then iterate over every field in that result set and print the text data.

This is what i have so far:

context = etree.iterparse(contentBuffer, tag='result')
for action, elem in context:
    print elem.tag, elem.data

Any help would be greatly appreciated.

EDIT
Here is the code that i came up with. It seems a bit clunky having to call getparent() twice to read the attribute of corresponding text value. Is there a better way to do this?

for action, elem in context:
    list = elem.xpath('//text')
    print "result set:"
    for item in list:
        field = item.getparent().getparent().attrib['k']
        value = item.text
        print "\t%s = %s"%(field, value)
  • 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-21T04:29:44+00:00Added an answer on May 21, 2026 at 4:29 am

    How about:

    import io
    import lxml.etree as ET
    
    content='''\
    <result>
        <field k='field1'>
            <value h='1'><text>text_value1</text></value>
        </field>
        <field k='field2'>
            <value><text>text_value2</text></value>
        </field>
        <field k='field3'>
            <value><text>some_text</text></value>
        </field>
    </result>'''
    
    contentBuffer=io.BytesIO(content)
    context = ET.iterparse(contentBuffer,tag='result')
    for action, elem in context:
        fields=elem.xpath('field/@k')
        values=elem.xpath('field/value/text/text()')
        for field,value in zip(fields,values):
            print('\t{f} = {v}'.format(f=field,v=value))
    

    which yields

    field1 = text_value1
    field2 = text_value2
    field3 = some_text
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

No related questions found

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.