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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T14:52:12+00:00 2026-06-11T14:52:12+00:00

I have an xml-file with a format similar to docx, i.e.: <w:r> <w:rPr> <w:sz

  • 0

I have an xml-file with a format similar to docx, i.e.:

<w:r>
  <w:rPr>
    <w:sz w:val="36"/>
    <w:szCs w:val="36"/>
  </w:rPr>
  <w:t>BIG_TEXT</w:t>
</w:r>

I need to get an index of BIG_TEXT in source xml, like:

from lxml import etree
text = open('/devel/tmp/doc2/word/document.xml', 'r').read()

root = etree.XML(text)

start = 0
for e in root.iter("*"):
    if e.text:
        offset = text.index(e.text, start)
        l = len(e.text)
        print 'Text "%s" at offset %s and len=%s' % (e.text, offset, l)
        start = offset + l

I can start a new search from position of current index + len(text), but is there another way? Element may have one character, w for example. It will find index of w, but not index of tag text w.

  • 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-11T14:52:13+00:00Added an answer on June 11, 2026 at 2:52 pm

    I was looking for a similar solution (indexing nodes in a big xml file for fast lookup).

    • AFAIK, lxml only offers sourceline, which is insufficient. Cf API : Original line number as found by the parser or None if unknown.
    • But expat provides the exact offset in the file : CurrentByteIndex.
      • Fetched from start_element handler, it returns tag’s start (ie '<') offset.
      • Fetched from char_data handler, it returns data’s start (ie 'B' in your example) offset.

    Example :

    import xml.parsers.expat
    
    # handler functions for parser events, and housekeeping.
    class handler :
       def __init__(self, current_parser) :
          #tag of interest
          self.TARGET_TAG = "w:t"
    
          #set up parser
          self.parser = current_parser
          self.parser.StartElementHandler  = self.start_element
          self.parser.EndElementHandler    = self.end_element
          self.parser.CharacterDataHandler = self.char_data
    
          self.target_tag_met = False
          self.index = None
    
       def start_element(self, name, attrs):
          self.target_tag_met = (name == self.TARGET_TAG)
    
       def end_element(self, name) :
          self.target_tag_met = False
    
       def char_data(self, data):
          if self.target_tag_met :
             self.index = self.parser.CurrentByteIndex
    
    #open file in binary mode for robuster byte offsets.
    xmlFile = open("so_test.xml", 'rb')
    
    p = xml.parsers.expat.ParserCreate()
    h = handler(p)
    
    p.ParseFile(xmlFile)
    print (h.index)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an XML file with format similar to: <root> <baby> <a>stuff</a> <b>stuff</b> <c>stuff</c>
I have an xml file in a format similar to this: <benchmark> <group> <id>1</id>
I have an XML file format that contains a structure of questions: <question id=q101>
I have an XML based file format that I'm using to store and load
I have a XML file with the following data format: <net NetName=abc attr1=123 attr2=234
I have an xml file in the following format: <food> <desert> cake <desert> </food>
I have an XSLT stylesheet that transforms an XML file to JSON format and
I have a xml file which I need to open with Microsoft Word 2007.
I have an XML file with a lot of nodes similar to the following
I have a XML file similar to the following: <a> <b value=a123 /> <b

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.