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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T07:33:05+00:00 2026-06-02T07:33:05+00:00

I need to parse some XML containing inline elements. The XML look, for example,

  • 0

I need to parse some XML containing inline elements. The XML look, for example, like this:

<section>
Fubar, I'm so fubar, fubar and even more <fref bar="baz">fubare</fref>. And yet more fubar.
</section>

If I iterate now over this structure with for elem in list(parent): ... I only get access to fref. If I now process fref, the surrounding text is of course lost, since text isn’t a real element.

Does anybody know of a way to properly address this issue?

  • 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-06-02T07:33:07+00:00Added an answer on June 2, 2026 at 7:33 am

    The following shows how to achieve this with lxml.

    >>> from lxml.etree import fromstring
    >>> tree = fromstring('''<section> Fubar, I'm so fubar, fubar and even more <fref bar="baz">fubare</fref>. And yet more fubar. </section>''')
    >>> elem = tree.xpath('/section/fref')[0]
    >>> elem.text
    'fubare'
    >>> elem.tail
    '. And yet more fubar. '
    >>> elem.getparent().text
    " Fubar, I'm so fubar, fubar and even more "
    

    From lxml.etree tutorial:

    If you want to read only the text, i.e. without any intermediate tags,
    you have to recursively concatenate all text and tail attributes in
    the correct order. Again, the tostring() function comes to the rescue,
    this time using the method keyword:

    >>> from lxml.etree import tostring
    >>> tostring(html, method="text")
    " Fubar, I'm so fubar, fubar and even more fubare. And yet more fubar. "
    

    There’s also an XPath way to do this, it’s described in the linked page.

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

Sidebar

Related Questions

I want to parse some XML that looks like this: <node> This is <child>
I need to parse a XML String so I can use some of the
I need to parse some XML that triggers an NSURLConnection. After the parsing finished,
I am trying to parse some XML that looks similar to this: <document> <headings>
I am currently parsing some xml that looks like this <Rows> <Row MANUFACTURERID=76 MANUFACTURERNAME=Fondont
I am doing a project to parse some data from the XML. For example,
I have some XML code that looks like this <SEARCHRESULTS> <FUNCTION name=BarGraph> <PARAMETER name=numList></PARAMETER>
I'm modifying some legacy code and need to parse/write to XML with C#. I'm
I need to download and parse some XML data, and to store it in
I have a problem right now, I need to parse an XML but some

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.