그나저나 망할(?) <를 <로 바꿔버리는 바람에 귀찮아지네...
특수문자를 건드리지 않고 바이패스 하는법 없으려나?
#include <stdio.h>
#include <string.h>
#include <expat.h>
#ifdef XML_LARGE_SIZE
# if defined(XML_USE_MSC_EXTENSIONS) && _MSC_VER < 1400
# define XML_FMT_INT_MOD "I64"
# else
# define XML_FMT_INT_MOD "ll"
# endif
#else
# define XML_FMT_INT_MOD "l"
#endif
#ifdef XML_UNICODE_WCHAR_T
# define XML_FMT_STR "ls"
#else
# define XML_FMT_STR "s"
#endif
#define BUFFSIZE 8192
char Buff[BUFFSIZE];
char Buff_con[BUFFSIZE];
int Depth;
XML_Parser p;
static void XMLCALL
start(void *data, const XML_Char *el, const XML_Char **attr)
{
int i;
(void)data;
for (i = 0; i < Depth; i++)
printf("\t");
printf("<%" XML_FMT_STR, el);
for (i = 0; attr[i]; i += 2) {
printf(" %" XML_FMT_STR "=\"%" XML_FMT_STR "\"", attr[i], attr[i + 1]);
}
printf(">");
// printf(">\n");
Depth++;
}
static void XMLCALL
data(void *data, const XML_Char *s, int len)
{
if(len > 0)
{
memcpy(Buff_con, s, len);
Buff_con[len] = '\0';
printf("%s", Buff_con);
}
}
static void XMLCALL
end(void *data, const XML_Char *el)
{
int i;
(void)data;
(void)el;
// for (i = 0; i < Depth; i++)
// printf("\t");
printf("</%" XML_FMT_STR, el);
printf(">");
// printf(">\n");
Depth--;
}
int
main(int argc, char *argv[])
{
// XML_Parser p = XML_ParserCreate(NULL);
p = XML_ParserCreate(NULL);
(void)argc;
(void)argv;
if (! p) {
fprintf(stderr, "Couldn't allocate memory for parser\n");
exit(-1);
}
XML_SetElementHandler(p, start, end);
XML_SetCharacterDataHandler(p, data);
XML_SetParamEntityParsing(p, XML_PARAM_ENTITY_PARSING_NEVER);
for (;;) {
int done;
int len;
len = (int)fread(Buff, 1, BUFFSIZE, stdin);
if (ferror(stdin)) {
fprintf(stderr, "Read error\n");
exit(-1);
}
done = feof(stdin);
if (XML_Parse(p, Buff, len, done) == XML_STATUS_ERROR) {
fprintf(stderr,
"Parse error at line %" XML_FMT_INT_MOD "u:\n%" XML_FMT_STR "\n",
XML_GetCurrentLineNumber(p),
XML_ErrorString(XML_GetErrorCode(p)));
exit(-1);
}
if (done)
break;
}
XML_ParserFree(p);
return 0;
}
start / end
[링크 : https://github.com/libexpat/libexpat/blob/master/expat/examples/elements.c]
data
[링크 : https://stackoverflow.com/questions/609376/geting-xml-data-using-xml-parser-expat]