[Expat-discuss] buffer get fulled
jayesh
jayesh at cins.unipune.ernet.in
Tue Aug 17 13:48:05 CEST 2004
Hi friends,
I am writing a small program for xml parsing, I used
source code from examples of expat
I have specified my static buffer size of 1024, It
parses well but as soon as the size of the file increases it is not able
to parse
Please give me the solution
Here is my code:
#include <stdio.h>
#include <expat.h>
#include <string>
#include <iostream>
#include <sstream>
#define BUFFSIZE 1024
using namespace std;
class ParseXml
{
public:
XML_Parser parser;
static string s_line;
static string p_str[20];
static int i_count;
static void xmlstart(void *userData, const char *el,const char **attr)
{
int i;
s_line = "";
for (i = 0; attr[i]; i += 2)
{
s_line+=attr[i+1];
s_line+=" ";
}
p_str[i_count++]=s_line;
int *depthPtr=(int *)userData;
*depthPtr++;
}
static void endElement(void *userData, const char *name)
{
int *depthPtr = (int *)userData;
*depthPtr -= 1;
}
string *parseNode()
{
parser = XML_ParserCreate(NULL);
FILE *fr;
long buffsize;
fr=fopen("./configuration.xml","r");
fseek(fr,0,SEEK_END);
buffsize=ftell(fr);
printf("%ld",buffsize);
fclose(fr);
buffsize=buffsize+200;
char Buff[buffsize];
int done;
int depth = 0,k = 0;
XML_SetUserData(parser, &depth);
XML_SetElementHandler(parser,xmlstart,endElement);
for (;;) {
int done;
int len;
fr = fopen("./configuration.xml", "r");
len = fread(Buff, 1,buffsize, fr);
if (ferror(fr)) {
printf("Read error");
return 0;
}
done = feof(fr);
if (XML_Parse(parser, Buff, len, done) == XML_STATUS_ERROR) {
fprintf(stderr, "Parse error at line %d:\n%s\n",
XML_GetCurrentLineNumber(parser),
XML_ErrorString(XML_GetErrorCode(parser)));
return 0;
}
if (done)
{
fclose(fr);
XML_ParserFree(parser);
p_str[i_count]="stop";
return p_str;
}
}
}
};
string ParseXml :: s_line = "" ;
int ParseXml :: i_count = 0 ;
string ParseXml :: p_str[20] ;
int main()
{
int k=0;
string *temp=(string *)malloc(110);
ParseXml p1;
temp=p1.parseNode();
/* while(temp[k]!="stop")
{
std::cout << temp[k] << endl;
k++;
}
*/
exit(0);
}
With regards,
Jayesh shah
More information about the Expat-discuss
mailing list