Search code examples
clibxml2seekftell

LibXML2 + pull parser (stax) : stream position (ftell) of the event ?


I'd like to create an index and later, access some specific parts of a huge xml file, so I need to get the offset ( ftell ... ) for some 'startElement' Events.

Using the pull parser (stax) interface of libxml2 ( http://www.xmlsoft.org/xmlreader.html ) is it possible to get the offset in the stream of an event using libxml2 ?


Solution

  • Use the function xmlTextReaderByteConsumed:

    long xmlTextReaderByteConsumed (xmlTextReaderPtr reader)
    

    This function provides the current index of the parser used by the reader, relative to the start of the current entity. This function actually just wraps a call to xmlBytesConsumed() for the parser context associated with the reader. See xmlBytesConsumed() for more information.

    reader: an XML reader
    Returns: the index in bytes from the beginning of the entity or -1 in case the index could not be computed.