1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90
|
#include <stdio.h>
#include <stdlib.h>
#include <expat.h>
#if defined(__amigaos__) && defined(__USE_INLINE__)
#include <proto/expat.h>
#endif
#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
#define BUFFSIZE 8192
char buff[BUFFSIZE];
void XMLCALL debutElement (void *userdata, const char * el, const char** attr){
int i = 0;
fprintf(stdout,"[%s]={",el);
while(*(attr + i) != NULL){
fprintf(stdout,"%s = %s, ",attr[i],attr[i+1]);
i+=2;
}
fprintf(stdout,"}\n");
}
void XMLCALL finElement(void *userdata, const char *el){
}
int main(int argc, char *argv[]){
FILE *file;
char *path;
XML_Parser p;
int taille;
int done;
if(argc != 1){
fprintf(stderr,"Usage : expat <chemin fichier>");
exit(1);
}
path = argv[1];
file = fopen(path, "r");
if( file == NULL){
fprintf(stdout, "Impossible d'ouvrir le fichier\n");
exit(1);
}
p = XML_ParserCreate(NULL);
if( p == NULL){
fprintf(stderr,"parser init error\n");
exit(1);
}
XML_SetElementHandler(p,debutElement,finElement);
while(done){
taille = (int) fread(buff,1,BUFFSIZE,file);
if(ferror(file)){
fprintf(stderr,"fread error\n");
exit(-1);
}
done = feof(file);
if(XML_Parse(p,buff,taille,done) == XML_STATUS_ERROR){
fprintf(stderr,"Error at line %" XML_FMT_INT_MOD "u:\n%s\n",XML_GetCurrentLineNumber(p),XML_ErrorString(XML_GetErrorCode(p)));
exit(-1);
}
}
XML_ParserFree(p);
fclose(file);
return 0;
} |
Partager