[Expat-discuss] Re: hi.. THx for ur help and patience.

Josh Martin Josh.Martin@abq.sc.philips.com
Mon Jul 8 14:43:02 2002


Hi Vijay,

I'm sorry you're still having problems, and for the slow reply.  I think I see 
your problem though.  Am I crazy, or do you have a carriage return in the middle 
of your <?xml?> tag?  If you do, and it looks like you do, that is probably why 
the code is issuing an error there.  I'm not very familiar with the UTF 
encodings, but I think that having it parse your document in UTF-8 is causing it 
to misinterperet that carriage return, as you should normally be able to use a 
return any place you can use other normal whitespace.  Take that out and try it 
again.  Since I'm pretty sure that's the culprit, but I'm not confident about my 
interperetation of why the return caused an error, I'm going to post this to the 
expat mailing list as well in hopes that someone more knowledgable in that area 
can help.

Also, I'm still uncomfortable with your use of the "done" variable in the main 
parsing loop, as I am quite certain it will break once you start reading in 
multi-line documents.

If this doesn't help, you might want to try not specifying the encoding type in 
the document and in the call to XML_ParserCreate().  If you still get the same 
error message after that, I don't think I can help, but send another copy of 
your code anyway.  Good luck.

 - Josh Martin

> Hi Josh,
> 
> Thank you very much for ur help and patience. I am attaching the current
> code which I have and also the error. I made the required changes u had sent
> me but I still get an error.. The output along with the "^" charecter is
> attached Please tell me if u can spot as to what is causing the error.
> 
> 
> /*-------------------------------code---------------------------------------
> ---*/
> #include <stdio.h>
> #include <expat.h>
> 
> #define BUFFSIZE	2046
> 
> /* This is the structure passed bewteen handlers */
> #define XML_STACK_SIZE   50
> #define XML_ELEMENT_SIZE 50
> typedef struct
> {
>    int   Depth;
>    char  stack[XML_STACK_SIZE][XML_ELEMENT_SIZE+1];
> } UserData;
> 
> char totElements[20][100];
> char totAttributes[20][100];
> int count = 0;
> 
> void start(void *data, const char *el, const char **attr)
> {
>    int i;
>    UserData *pUData = (UserData *)data;
> 
>    printf("el = %s\n", el);
>    /* Store this in our stack */
>    if (pUData->Depth < XML_STACK_SIZE)
>    {
>       strncpy(pUData->stack[pUData->Depth], el, XML_ELEMENT_SIZE);
>    }
>    (pUData->Depth)++;
> }  /* End of start handler */
> 
> 
> void end(void *data, const char *el)
> {
>    int i;
>    UserData *pUData = (UserData *)data;
> 
>    (pUData->Depth)--;
>    if (pUData->Depth < XML_STACK_SIZE)
>    {
>       /* Clear this entry in the stack */
>       pUData->stack[pUData->Depth][0] = 0;
>    }
> }  /* End of end handler */
> 
> 
> void charHdlr(void *data, const char *s, int len)
> {
>    UserData *pUData = (UserData *)data;
>    char tmp[80];
>    char *tmp1;
>    int i, idx, left;
>    char *pS = (char *)s;
> 
>    /* The input string is not NULL terminated */
>    if (len <= 0)
>       return;
> 
>    /* Character data is any data within element tags.  This includes
>       newlines and whitespace, etc between nested elements too.
>    */
>    while ((*pS == ' ') || (*pS == '\n') || (*pS == '\t'))
>    {
>       pS++;
>       len--;
> 
>       /* Check if we run out of characters */
>       if (len <= 0)
>          return;
>    }
>    for (i = 0; i < pUData->Depth; i++){
>       tmp1 = pUData->stack[i];
>    }
>    strcpy(totElements[count],tmp1);//,strlen(tmp1));
>    /* Now print out our element too */
>    idx  = 0;
>    left = len;
>    while (left > 80)
>    {
>       memcpy (&tmp[0], &pS[idx], 79);
>       tmp[80] = 0;
>       printf ("\n%s", tmp);
>       left -= 80;
>       idx  += 80;
>    }
>    memcpy (&tmp[0], &pS[idx], left);
>    tmp[left] = 0;
>    strcpy(totAttributes[count],&tmp[0]);
>    count++;
> }  /* End of charHdlr handler */
> 
> 
> main(int argc, char **argv)
> {
>    XML_Parser p;
>    UserData   u;
>    void       *Buff;
>    int x = 0;
>    char dtr[] = "<?xml version=\'1.0\' encoding=\'utf-8\'
> ?><msg><head><RoundTripInfo>06/17/200207:50:21</RoundTripInfo></head></msg>"
> ;
>    int len = strlen(dtr);
>    memset (&u, 0, sizeof(UserData));
> 
>    printf("size of = %d user data = %d\n",len,sizeof(UserData));
>    p = XML_ParserCreate("utf-8");
>    if (! p)
>    {
>       fprintf(stderr, "Couldn't allocate memory for parser\n");
>       exit(-1);
>    }
> 
>    XML_SetElementHandler(p, start, end);
>    XML_SetCharacterDataHandler(p, charHdlr);
>    XML_SetUserData(p, &u);
>    int done = 0;
>    printf("Document Recd = %s\n", dtr);
>    for (;;)
>    {
>       int bytes_read;
>       /* Last parameter indicates if this is the last chunk or not */
>       
>       if(!XML_Parse(p,dtr,strlen(dtr),1)) //sizeof(dtr),0))
>       {
> 	 int b_index = 0;
>          b_index = XML_GetCurrentByteIndex(p);
>          strtok(&dtr[b_index-XML_GetCurrentColumnNumber(p)], "\n\r");
>          fprintf(stderr, "Parse error at line %1$d: %2$s\n%3$s\n%4$*5$c\n",
>               XML_GetCurrentLineNumber(p),
>               XML_ErrorString(XML_GetErrorCode(p)),
> 	      &(dtr[b_index-XML_GetCurrentColumnNumber(p)]),
> 	      '^', XML_GetCurrentColumnNumber(p)+1);
>          XML_ParserFree(p);
>          exit(-1);
>       }
>       done++;
>       if (done == len-1)
>          break;
>    }
>    /* I think this frees any buffers obtained from XML_GetBuffer too, if
> required */
>    XML_ParserFree (p);
> 
> }  /* End of main */
> /*-------------------------------end of
> code------------------------------------------*/
> 
> 
> output :
> 
> size of = 112 user data = 2556
> Document Recd = <?xml version='1.0' encoding='utf-8'
> ?><msg><head><RoundTripInfo>06/17/200207:50:21</RoundTripInfo></head></msg>
> el = msg
> el = head
> el = RoundTripInfo
> Parse error at line 1: junk after document element
> <?xml version='1.0' encoding='utf-8'
> ?><msg><head><RoundTripInfo>06/17/200207:50:21</RoundTripInfo></head></msg>
> ^
> 
> Vijay Naidu
> System Analyst
> Office : 814-274-6526
> Cell : 814-932-5327
> Pager : 800-541-0931
> ----------------------------------------------------------------------------
> ------------
> Smile is a curve that makes everything straight. Keep smiling
> Experience, my friends, is the hardest kind of teacher. 
> It gives you a test first and the lesson afterwards
> ----------------------------------------------------------------------------
> ------------