[Expat-discuss] Getting xml element data...
Nikola
root.admin1 at zg.t-com.hr
Fri Jul 21 03:52:56 CEST 2006
Hello,
I am trying to parse simple xml file and process text value in elements
(eg. <e>some text</e>).
but when my "processText" functions gets called on text element....it
also contains ending tag.....
How can I get only text....i am sure that library is not designed to
return ending tag in text value
of xml element.
I accept posibility that I am doing soemthing wrong.....
Here are the files.
Tnx.
test.xml
----------------------------------------------------
<?xml version="1.0" ?>
<main>
<data> some data </data>
</main>
----------------------------------------------------
with the following code...
parse_test.c
----------------------------------------------------
#include <stdio.h>
#include <expat.h>
static int status=0;
const char *gname;
static void XMLCALL
startElement(void *userData, const char *name, const char **atts)
{
if(!strcmp(name,"data")) status = 1;
printf("<%s>\n",name);
gname = name;
}
static void XMLCALL
endElement(void *userData, const char *name)
{
printf("</%s>\n",name);
}
static void XMLCALL
processText(void *userData, const char *data, int len)
{
if(status && !strcmp(gname,"data")){
printf("[%s]",data);
status = 0;
}
}
int main(int argc, char *argv[])
{
char buf[BUFSIZ];
XML_Parser parser = XML_ParserCreate(NULL);
int done=0;
int depth = 0;
XML_SetUserData(parser, &depth);
XML_SetElementHandler(parser, startElement, endElement);
XML_SetCharacterDataHandler(parser,processText);
FILE *fp;
fp = fopen(argv[1],"r");
while(fgets(buf,BUFSIZ,fp)) {
if(feof(fp)) done=1;
if (XML_Parse(parser, buf, strlen(buf), done) == XML_STATUS_ERROR) {
fprintf(stderr,
"%s at line %d\n",
XML_ErrorString(XML_GetErrorCode(parser)),
XML_GetCurrentLineNumber(parser));
return 1;
}
}
fclose(fp);
XML_ParserFree(parser);
return 0;
}
----------------------------------------------------
More information about the Expat-discuss
mailing list