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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T03:20:59+00:00 2026-05-16T03:20:59+00:00

In Python 2.6 using ElementTree, what’s a good way to fetch the XML (as

  • 0

In Python 2.6 using ElementTree, what’s a good way to fetch the XML (as a string) inside a particular element, like what you can do in HTML and javascript with innerHTML?

Here’s a simplified sample of the XML node I am starting with:

<label attr="foo" attr2="bar">This is some text <a href="foo.htm">and a link</a> in embedded HTML</label>

I’d like to end up with this string:

This is some text <a href="foo.htm">and a link</a> in embedded HTML

I’ve tried iterating over the parent node and concatenating the tostring() of the children, but that gave me only the subnodes:

# returns only subnodes (e.g. <a href="foo.htm">and a link</a>)
''.join([et.tostring(sub, encoding="utf-8") for sub in node])

I can hack up a solution using regular expressions, but was hoping there’d be something less hacky than this:

re.sub("</\w+?>\s*?$", "", re.sub("^\s*?<\w*?>", "", et.tostring(node, encoding="utf-8")))
  • 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-16T03:21:00+00:00Added an answer on May 16, 2026 at 3:21 am

    How about:

    from xml.etree import ElementTree as ET
    
    xml = '<root>start here<child1>some text<sub1/>here</child1>and<child2>here as well<sub2/><sub3/></child2>end here</root>'
    root = ET.fromstring(xml)
    
    def content(tag):
        return tag.text + ''.join(ET.tostring(e) for e in tag)
    
    print content(root)
    print content(root.find('child2'))
    

    Resulting in:

    start here<child1>some text<sub1 />here</child1>and<child2>here as well<sub2 /><sub3 /></child2>end here
    here as well<sub2 /><sub3 />
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm parsing an xml file using Python's ElementTree, like that: et = ElementTree(file=file(test.xml)) test.xml
I am experiencing some trouble using the Python 2.6.5 xml.etree.ElementTree library. In particular, if
I am using the Python ElementTree module to manipulate HTML. I want to emphasize
I'm learning Python using Learn Python The Hard Way . It is very good
I have to write an xml file using python standard modules (not using elementtree,
I am using Python's elementtree library to parse an .XML file that I exported
I'm trying to create XML using the ElementTree object structure in python. It all
I'm using Python's ElementTree to parse xml files. I have a findall to find
I'm using Python's xml.etree.ElementTree to do some XML parsing on a file. However, I
I'm using Python with ElementTree to parse an XML file. I want to be

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.