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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T15:55:29+00:00 2026-05-15T15:55:29+00:00

I am working with potentially huge XML files containing complex trace information from on

  • 0

I am working with potentially huge XML files containing complex trace information from on of my projects.

I would like to build indexes for those XML files so that one can quickly find sub sections of the XML document without having to load it all into memory.

If I have created a “shelve” index that could contains information like “books for author Joe” are at offsets [22322, 35446, 54545] then I can just open the xml file like a regular text file and seek to those offsets and then had that to one of the DOM parser that takes a file or strings.

The part that I have not figured out yet is how to quickly parse the XML and create such an index.

So what I need as a fast SAX parser that allows me to find the start offset of tags in the file together with the start events. So I can parse a subsection of the XML together with the starting point into the document, extract the key information and store the key and offset in the shelve index.

  • 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-15T15:55:30+00:00Added an answer on May 15, 2026 at 3:55 pm

    Since locators return line and column numbers in lieu of offset, you need a little wrapping to track line ends — a simplified example (could have some offbyones;-)…:

    import cStringIO
    import re
    from xml import sax
    from xml.sax import handler
    
    relinend = re.compile(r'\n')
    
    txt = '''<foo>
                <tit>Bar</tit>
            <baz>whatever</baz>
         </foo>'''
    stm = cStringIO.StringIO(txt)
    
    class LocatingWrapper(object):
        def __init__(self, f):
            self.f = f
            self.linelocs = []
            self.curoffs = 0
    
        def read(self, *a):
            data = self.f.read(*a)
            linends = (m.start() for m in relinend.finditer(data))
            self.linelocs.extend(x + self.curoffs for x in linends)
            self.curoffs += len(data)
            return data
    
        def where(self, loc):
            return self.linelocs[loc.getLineNumber() - 1] + loc.getColumnNumber()
    
    locstm = LocatingWrapper(stm)
    
    class Handler(handler.ContentHandler):
        def setDocumentLocator(self, loc):
            self.loc = loc
        def startElement(self, name, attrs):
            print '%s@%s:%s (%s)' % (name, 
                                     self.loc.getLineNumber(),
                                     self.loc.getColumnNumber(),
                                     locstm.where(self.loc))
    
    sax.parse(locstm, Handler())
    

    Of course you don’t need to keep all of the linelocs around — to save memory, you can drop “old” ones (below the latest one queried) but then you need to make linelocs a dict, etc.

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

Sidebar

Related Questions

What could potentially stop an AJAX call from working on the host server, when
I'm working on a project that currently has zero users but we would like
I'm working on a solution to a problem where users could potentially access images
I'm working on a fairly large web site built in PHP that will potentially
I'm working on several processes which could potentially run into problems that must be
At work, I have started working on a program that can potentially generate hundreds
A project I'm working on potentially entails storing large amounts (e.g. ~5GB) of binary
I'm working on a desktop application that may potentially be deployed world-wide. My biggest
I'm working on a Java application that collects various bits of information on network
I'm working on a paper about multi-platform programming and I'd like to include sections

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.