[Expat-discuss] (no subject)
Ponnusamy.Anand at choicepoint.com
Ponnusamy.Anand at choicepoint.com
Tue Oct 28 16:34:07 EST 2003
This is the application code (API) we use to parse the XML document.
#include <stdio.h>
#include <ctype.h>
#include "../xmlparse/xmlparse.h"
#include "/export/home/expat/work/include/XMLParseAPI.h"
/*----------------------------------------------------------------------------*/
#define SUCCESS 0
#define FAILURE -1
#define TRUE 1
#define FALSE 0
/*----------------------------------------------------------------------------*/
static char *elementname = "";
static int errorlinenumber = 0;
static char errorlinetext[256] = "";
/*----------------------------------------------------------------------------*/
int (*elementvaluehandler)(char *elementname, char *s, int len) = NULL;
/*----------------------------------------------------------------------------*/
char *
XMLGetError(int *linenumber)
{
*linenumber = errorlinenumber;
return errorlinetext;
}
/*----------------------------------------------------------------------------*/
void
XMLSetElementValueHandler(int (*f)(char *, char *, int))
{
elementvaluehandler = f;
}
/*----------------------------------------------------------------------------*/
static int
trim_length_text(char *text, int *len)
{
register int ii;
for (ii = 0; ii < *len; ii++)
if (text[ii] != ' ' && text[ii] != '\t' && text[ii] != '\n')
return *len;
*len = 0;
return *len;
}
/*----------------------------------------------------------------------------*/
void
XMLStartElementHandler(void *data, const char *el, const char **attr)
{
if (* el) elementname = (char *) el;
}
/*----------------------------------------------------------------------------*/
void
XMLEndElementHandler(void *data, const char *el)
{
/* leaving what ? */
printf("%s\n", el);
}
/*----------------------------------------------------------------------------*/
void
XMLCharacterHandler(void *data, const char *s, int len)
{
trim_length_text((char *) s, &len);
if (len)
{
elementvaluehandler(elementname, (char *) s, len);
/*----------------------------------------------------------------------------*/
int
XMLElementParse(char *xml, void (*xmlstartelementhandler)(void *, const
char *, const char **),
int (*xmlelementvaluehandler)(char *, char *,
int),
void (*xmlendelementhandler) (void *, const
char *))
int rc = SUCCESS;
const char *encoding = "ISO-8859-1";
XML_Parser p;
errorlinenumber = 0;
memset(errorlinetext, '\0', sizeof(errorlinetext));
p = XML_ParserCreate(encoding);
if (!p)
{
return FAILURE;
}
XML_UseParserAsHandlerArg(p);
XML_SetCharacterDataHandler(p, XMLCharacterHandler);
XML_SetElementHandler(p, xmlstartelementhandler, xmlendelementhandler);
XMLSetElementValueHandler(xmlelementvaluehandler);
if (!XML_Parse(p, xml, strlen(xml), TRUE))
{
errorlinenumber = XML_GetCurrentLineNumber(p);
strncpy(errorlinetext, XML_ErrorString(XML_GetErrorCode(p)), 255);
rc = FAILURE;
}
XML_ParserFree(p);
return rc;
}
Regards,
P. Anand
(770)619-8319 (Office)
(678)576-8604 (Mobile)
"Fred L. Drake,
Jr." To: Ponnusamy.Anand at choicepoint.com
<fdrake at acm.org> cc: expat-discuss at libexpat.org
Subject: Re: [Expat-discuss] (no subject)
10/28/2003 04:17
PM
Ponnusamy.Anand at choicepoint.com writes:
> Can someone please explain how I need to get around this special
character
> problem?
I suspect you're only looking at the result of a single call to your
character data handler, but can't be sure without seeing your code.
The character data handler should accumulate data at each successive
call; the data will be available when the end-element handler is
called.
I'll be expanding the documentation related to this, as this is a
common stumbling block. For now, it is mentioned in the documentation
for XML_SetCharacterDataHandler().
-Fred
--
Fred L. Drake, Jr. <fdrake at acm.org>
PythonLabs at Zope Corporation
More information about the Expat-discuss
mailing list