Question

I'm writing xml parser using expat . just saw this :

XML_SetElementHandler
     XML_Parser parser,
     XML_StartElementHandler start,
     XML_EndElementHandler end

I don't understand what should it do I see that it's a pointer to a function :

typedef void (*XML_StartElementHandler)(void *userData,
                    const XML_Char *name,
                    const XML_Char **atts);

but I can't find anywhere what should it do would love to get some help!

Was it helpful?

Solution

You give the parser (pointers to) two functions, one of which gets called when the parser sees the start of an XML element (i.e. once it's read the start tag), and the other of which gets called when it sees the end of the element (i.e. reads the end tag) (you really have to understand the difference between elements and tags in order to do XML parsing).

The function that the parser calls at the start of an element gets called with three arguments:

  1. A generic (void *) pointer to whatever you initially passed in to the parser as "user data" (if you in fact did pass it). This is whatever you want to use to keep state between callbacks.
  2. A pointer to a character array (i.e. string) containing the text of the element's name.
  3. A pointer to an array of strings (OK, I'm not being pedantically correct here) with one member for each part of the attribute-value pair associated with the element (i.e. the attributes listed in the start tag). So, if atts[i] is the attribute, then atts[i+1] is its value.

The function that gets called for the end of an element has a shorter argument signature (again IIRC) since it won't be getting an attribute list.

For most commonly used XML, you'll also need a character-handler function to process character data (i.e. the stuff that comes between the start tag and end tag) and you'll need to repeatedly remind yourself that it will not, in general, be called with all the character data at once; that data may come in several separate "chunks".

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top