M
Maarten Verhage
Hi All,
I've made a modified version of the Expat elements.c example. What I
want the program to do is to recognize a hardcoded
element-name/tag-name and then execute some code which has access to
the data(e.g. "Maarten Verhage" in XML file).
The current situation (problem) is:
1. The XML parser calls the function startElement when it finds a tag,
in this function I don't have access to the data between the tags.
2. The XML parser calls the function charHandler when it finds data,
in this function I don't have access to the tag-name.
Thank you!
-- below is the code I have now
-- start of XML file
<?xml version="1.0"?>
<person>
<name>Maarten Verhage</name>
<age>24</age>
</person>
-- end of XML file
-- start of C file
#include <stdio.h>
#include <conio.h>
#include "expat.h"
static void XMLCALL startElement(void *userData, const char *name,
const char **atts)
{
int i;
int *depthPtr = userData;
for (i = 0; i < *depthPtr; i++)
{
putchar('\t');
}
// print elementname
puts(name);
*depthPtr += 1;
}
static void XMLCALL endElement(void *userData, const char *name)
{
int *depthPtr = userData;
*depthPtr -= 1;
}
static void XMLCALL charHandler(void *userData, const XML_Char *s, int
len)
{
int i;
for (i=0; i < len; i++)
{
fprintf(stdout, "%c", s);
}
}
int main(int argc, char *argv[])
{
FILE *xmlfile;
char buf[BUFSIZ];
XML_Parser parser = XML_ParserCreate(NULL);
int done;
int depth = 0;
XML_SetUserData(parser, &depth);
XML_SetElementHandler(parser, startElement, endElement);
XML_SetCharacterDataHandler(parser, charHandler);
// open XML file
if ((xmlfile = fopen("xml_test.xml", "r")) == NULL)
{
fprintf(stderr, "Error opening xml_test.xml\n");
getch();
return -1;
}
do
{
size_t len = fread(buf, 1, sizeof(buf), xmlfile);
done = len < sizeof(buf);
if (XML_Parse(parser, buf, len, done) == XML_STATUS_ERROR)
{
fprintf(stderr,
"%s at line %d\n",
XML_ErrorString(XML_GetErrorCode(parser)),
XML_GetCurrentLineNumber(parser));
return 1;
}
} while (!done);
XML_ParserFree(parser);
// close the XML file
fclose(xmlfile);
getch();
return 0;
}
-- end of C file
I've made a modified version of the Expat elements.c example. What I
want the program to do is to recognize a hardcoded
element-name/tag-name and then execute some code which has access to
the data(e.g. "Maarten Verhage" in XML file).
The current situation (problem) is:
1. The XML parser calls the function startElement when it finds a tag,
in this function I don't have access to the data between the tags.
2. The XML parser calls the function charHandler when it finds data,
in this function I don't have access to the tag-name.
Thank you!
-- below is the code I have now
-- start of XML file
<?xml version="1.0"?>
<person>
<name>Maarten Verhage</name>
<age>24</age>
</person>
-- end of XML file
-- start of C file
#include <stdio.h>
#include <conio.h>
#include "expat.h"
static void XMLCALL startElement(void *userData, const char *name,
const char **atts)
{
int i;
int *depthPtr = userData;
for (i = 0; i < *depthPtr; i++)
{
putchar('\t');
}
// print elementname
puts(name);
*depthPtr += 1;
}
static void XMLCALL endElement(void *userData, const char *name)
{
int *depthPtr = userData;
*depthPtr -= 1;
}
static void XMLCALL charHandler(void *userData, const XML_Char *s, int
len)
{
int i;
for (i=0; i < len; i++)
{
fprintf(stdout, "%c", s);
}
}
int main(int argc, char *argv[])
{
FILE *xmlfile;
char buf[BUFSIZ];
XML_Parser parser = XML_ParserCreate(NULL);
int done;
int depth = 0;
XML_SetUserData(parser, &depth);
XML_SetElementHandler(parser, startElement, endElement);
XML_SetCharacterDataHandler(parser, charHandler);
// open XML file
if ((xmlfile = fopen("xml_test.xml", "r")) == NULL)
{
fprintf(stderr, "Error opening xml_test.xml\n");
getch();
return -1;
}
do
{
size_t len = fread(buf, 1, sizeof(buf), xmlfile);
done = len < sizeof(buf);
if (XML_Parse(parser, buf, len, done) == XML_STATUS_ERROR)
{
fprintf(stderr,
"%s at line %d\n",
XML_ErrorString(XML_GetErrorCode(parser)),
XML_GetCurrentLineNumber(parser));
return 1;
}
} while (!done);
XML_ParserFree(parser);
// close the XML file
fclose(xmlfile);
getch();
return 0;
}
-- end of C file