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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T18:55:11+00:00 2026-05-31T18:55:11+00:00

I have to parse a 1Gb XML file with a structure such as below

  • 0

I have to parse a 1Gb XML file with a structure such as below and extract the text within the tags “Author” and “Content”:

<Database>
    <BlogPost>
        <Date>MM/DD/YY</Date>
        <Author>Last Name, Name</Author>
        <Content>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Maecenas dictum dictum vehicula.</Content>
    </BlogPost>

    <BlogPost>
        <Date>MM/DD/YY</Date>
        <Author>Last Name, Name</Author>
        <Content>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Maecenas dictum dictum vehicula.</Content>
    </BlogPost>

    [...]

    <BlogPost>
        <Date>MM/DD/YY</Date>
        <Author>Last Name, Name</Author>
        <Content>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Maecenas dictum dictum vehicula.</Content>
    </BlogPost>
</Database>

So far I’ve tried two things: i) reading the whole file and going through it with .find(xmltag) and ii) parsing the xml file with lxml and iterparse().
The first option I’ve got it to work, but it is very slow. The second option I haven’t managed to get it off the ground.

Here’s part of what I have:

for event, element in etree.iterparse(path_to_file, tag="BlogPost"):
    if element.tag == "BlogPost":
        print element.text
    else:
        print 'Finished'

The result of that is only blank spaces, with no text in them.

I must be doing something wrong, but I can’t grasp it. Also, In case it wasn’t obvious enough, I am quite new to python and it is the first time I’m using lxml. Please, help!

  • 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-31T18:55:12+00:00Added an answer on May 31, 2026 at 6:55 pm
    for event, element in etree.iterparse(path_to_file, tag="BlogPost"):
      for child in element:
        print(child.tag, child.text)
        element.clear()
    

    the final clear will stop you from using too much memory.

    [update:] to get "everything between … as a string" i guess you want one of:

    for event, element in etree.iterparse(path_to_file, tag="BlogPost"):
      print(etree.tostring(element))
      element.clear()
    

    or

    for event, element in etree.iterparse(path_to_file, tag="BlogPost"):
      print(''.join([etree.tostring(child) for child in element]))
      element.clear()
    

    or perhaps even:

    for event, element in etree.iterparse(path_to_file, tag="BlogPost"):
      print(''.join([child.text for child in element]))
      element.clear()
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have to parse a tab-delimited text file with Ruby to extract some data
I'm working on #huge# text files (from 100mb to 1gb), I have to parse
I have to parse xml file and after parsing I have to out related
I have to parse some tables from an ASCII text file. Here's a partial
I have to parse a XML structure in JAVA using the SAX parser. The
I have a 1GB json file and i want to parse it using simplejson
I have to parse a XML configuration file. I decided to do the basic
I have to parse large XML file in C#. I use LINQ-to-XML. I have
i have to parse files logs (5gb) error logs (1gb) import data file (csv)
I have to read a text file and then to parse it, in C#

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.