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 1079827
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T21:55:51+00:00 2026-05-16T21:55:51+00:00

I am using ElementTree and cannot figure out if the childnode is text or

  • 0

I am using ElementTree and cannot figure out if the childnode is text or not. childelement.text does not seem to work as it gives false positive even on nodes which are not text nodes.

Any suggestions?

Example

<tr>
  <td><a href="sdas3">something for link</a></td>
  <td>tttttk</td>
  <td><a href="tyty">tyt for link</a></td>
</tr>

After parsing this xml file, I do this in Python:

for elem_main in container_trs: #elem_main is each tr
    elem0 = elem_main.getchildren()[0] #td[0]
    elem1 = elem_main.getchildren()[1] #td[1]

    elem0 = elem_main.getchildren()[0]
    print elem0.text

    elem1 = elem_main.getchildren()[1]
    print elem1.text

The above code does not output elem0.text; it is blank. I do see the elem1.text (that is, tttttk) in the output.

Update 2

I am actually building a dictionary. The text from the element with each so that I can sort the HTML table. How would I get the s in this code?

  • 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-16T21:55:52+00:00Added an answer on May 16, 2026 at 9:55 pm

    How about using the getiterator method to iterate through the all the descendant nodes:

    import xml.etree.ElementTree as xee
    
    content='''
    <tr>
      <td><a href="sdas3">something for link</a></td>
      <td>tttttk</td>
      <td><a href="tyty">tyt for link</a></td>
    </tr>
    '''
    
    def text_content(node):
        result=[]
        for elem in node.getiterator():
            text=elem.text
            if text and text.strip():
                result.append(text)
        return result
    
    container_trs=xee.fromstring(content)
    adict={}
    for elem in container_trs:
        adict[elem]=text_content(elem)
    print(adict)
    # {<Element td at b767e52c>: ['tttttk'], <Element td at b767e58c>: ['tyt for link'], <Element td at b767e36c>: ['something for link']}
    

    The loop for elem_main in container_trs: iterates through the children of cantainer_trs.

    In contrast, the loop for elem_main in container_trs.getiterator(): iteraters through container_trs itself, and its children, and grand-children, etc.

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

Sidebar

Related Questions

Hi I'm using ElementTree to parse out an xml feed from Kuler. I'm only
Im new using Python, now I have a problem using ElementTree and read out
I have to write an xml file using python standard modules (not using elementtree,
I am using ElementTree to parse a XML file. In some fields, there will
I'm trying to write an xml file with utf-8 encoded data using ElementTree like
Using the elementtree package in nodejs, I'm trying to verify the existence of a
I'm using ElementTree to generate some HTML, but I've run into the problem that
In Python 2.6 using ElementTree, what's a good way to fetch the XML (as
I'm learning lxml (after using ElementTree) and I'm baffled why .fromstring and .tostring do
I am using the Python ElementTree module to manipulate HTML. I want to emphasize

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.