[Expat-discuss] getting offsets of attributes

Fabio Venuti fabio at soi.city.ac.uk
Tue May 13 16:07:27 EDT 2003


Hello everybody,
sometime ago I asked whether it was possible to get the offset of attributes in some way similar to XML_GetCurrentByteIndex. 
The answer was no. 
So I found my own way to do it and would like to share. I am aware that there might be better (=more efficient, more robust) ways to do that.
Basically my idea is to get the file being parsed (assuming it's in stdin), then change the pointer to stdin so it points at the beginning of the current xml element, then get the whole element tag into a string, finally find the relative offset of the attributes inside the string.
Here is the code, added to the start handler (adapted from the code I used in my program, so there might be some mistakes, though I tried looking at it carefully...).

static void
start(void *data, const char *el, const char **attr)
{

/* Meaning of variables:
   current_pos = position in xml file being parsed
   xml_byte_proc = bytes already processed 
     Basically xml_byte_proc = BUFFSIZE * (Number of BUFFERS already processed)
   element_offset = absolute offset of current element
   element_tag = contains the text of the whole current element tag
   att_info[] = array containing attributes' offsets and lengths
*/

...
current_pos = ftell(stdin);
element_offset = XML_GetCurrentByteIndex(xmlp) + xml_byte_proc;
fseek(stdin, element_offset, SEEK_SET);
j=0;
while((element_tag[j++] = getc(stdin)) != '>');
element_tag[j] = '\0';
for (j=0; attr[j]; j += 2) {
  att_info[j] = strlen(element_tag) - strlen(strstr(element_tag, attr[j]));
  att_info[j+1] = strlen(attr[j+1]);
}
/* I'm not sure it's necessary to record first current_pos in stdin and then
   restore it at the end of the processing, but I do it just to be sure... */
fseek(stdin, current_pos, SEEK_SET);

It works for my needs.
Bye,

Fabio


More information about the Expat-discuss mailing list