[Expat-discuss] working with character data

Dustin Puryear dpuryear@usa.net
Tue, 17 Jul 2001 15:35:22 -0500


I am having some problems with an application that uses XML. (It is a 
part of a testing harness for Jabber, the XML-based Instant Messaging 
tool.) The problem is that it seems I'm losing messages at some point. I 
think it may be within jabberd, but I'm not so sure that my client isn't 
properly processing the XML messages.

Can I assume that the character data sent to the character data handler 
by expat is the entire message, or could it only be part of the message, 
and I need to push what I have on a stack and wait to see what I get next?

In other words, if I send:

<tag>a b c d</tag>

Can I assume that "a b c d" are sent to my handler or not? Will it 
possibly be sent as "a", then "b c", and then "d", or some other 
variation? My handler is quite simple, as shown below:

void char_data_hdlr(void *userdata, const XML_Char *s, int len)
{
         user_data_t *ud = userdata;
         char buf[MAX_XML_BUFSZ+1];
         struct timeval tv;
         reply_data_t *reply;
         int id;

         memcpy(buf, s, len);
         buf[len] = '\0';
         DPRINT("found message: %s\n", buf);

         /* scan for our start times at the beginning of the message */
         if (sscanf(buf, " %d %ld %ld ", &id, &(tv.tv_sec), 
&(tv.tv_usec)) == 3)
         {
                 reply = malloc(sizeof(reply_data_t));
                 if (reply == NULL)
                 {
                         perror("malloc()");
                         exit(EXIT_FAILURE);
                 }

                 DPRINT("char_data_hdlr(): adding buf = %s with sec = 
%ld and usec = %ld\n",
                                 buf, tv.tv_sec, tv.tv_usec);

                 reply->begin.tv_sec = tv.tv_sec;
                 reply->begin.tv_usec = tv.tv_usec;
                 reply->id = id;
                 list_add(&(ud->reply_list), (void *) reply);
         }
}

Here is how I setup expat:

        /* setup our XML parser */
         parser = XML_ParserCreate(NULL);
         XML_SetUserData(parser, &ud);
         XML_SetElementHandler(parser,
                         start_element_hdlr,
                         end_element_hdlr);
         XML_SetCharacterDataHandler(parser, char_data_hdlr);
         parser_done = 0;

I am using expat v1.2. Any help is appreciated.

Regards, Dustin

-- 
Dustin Puryear <dpuryear@usa.net>
http://members.telocity.com/~dpuryear
In the beginning the Universe was created.
This has been widely regarded as a bad move. - Douglas Adams