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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T14:37:25+00:00 2026-05-26T14:37:25+00:00

I am getting information from a large xml file and I am using python

  • 0

I am getting information from a large xml file and I am using python with lxml target parsing interface to do it. I would like the possibility to set a limit after which parsing stops. Here is some code:

Parser target code:

class TitleTarget(object):
    def __init__(self,  limit=None):
        self.limit = limit
        self.counter = 0

    def start(self, tag, attrib):
        if self.limit and self.counter > self.limit:
            #### BREAK HERE ####
            return False
        #doProcessing(attrib)
        self.counter = self.counter + 1

    def end(self, tag):
        pass

    def data(self, data):
        pass

def close(self):
    pass

Code initiating the parsing:

parser = etree.XMLParser(target = TitleTarget(limit)) 
etree.parse(file, parser)  

I know that the processing goes to the “BREAK HERE” -line, but I haven’t found any method to stop the parsing. I have tried returning True, False, [], and raising Error, none seem to work. It always processes until the file ends.

Is there a way to stop processing bu using this method.

  • 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-26T14:37:25+00:00Added an answer on May 26, 2026 at 2:37 pm

    Instead of using etree.parse(file, parser), you can loop over the lines in file and call parser.feed on each line. This gives you control over when to break out of the loop.

    Now you can just set a self.done=True in the target, and test for target.done in the feed loop:


    import lxml.etree as ET
    class HaltingTarget(object):
        def __init__(self, limit=None):
            self.done=False
            self.limit=limit
            self.counter=0
            self.result=[]
        def start(self, tag, attrib):
            if self.limit and self.counter>self.limit:
                self.done=True
                return
            if attrib:
                self.result.append(attrib)
            self.counter+=1
        def end(self, tag):
            pass
        def data(self, data):
            pass
        def comment(self, text):
            pass
        def close(self):
            return
    def halt_parser():
        content='''\
            <node1>
              <Title a1="x1"> My Title </Title>
              <node2 a1="x2"> ... </node2>
              <node2 a1="x1"> ... </node2>
            </node1>
            '''
        target=HaltingTarget()
        parser=ET.XMLParser(target=target)
        for line in content.splitlines():
            parser.feed(line.strip())
            if target.done: break
        # We can't call parser.close() since the XML we've fed it is probably 
        # incomplete. We don't plan to use `parser` anymore, so delete it.
        del parser
        print(target.result)
        # [{'a1': u'x1'}, {'a1': u'x2'}, {'a1': u'x1'}]
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am getting a large text file of updated information from a customer that
I'm developing a flash application that is getting information from a remote server using
Here's the problem: In C# I'm getting information from a legacy ACCESS database. .NET
I have an iPhone application which basically is getting information from an API (in
I'm having trouble getting any information to display from this query. Anyone know where
Windows has GetFileTime API which allows getting file time information by open file handle.
So I'm extracting the lines that I want from this larger file using this
I'm getting information from database, saving it in array and echoing it in a
I'm getting information from the database (MongoDB) and then converting it to an array
I'm trying to use JSON, I'm getting the information from the server so I've

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.