I have a strange problem when I parse utf-8 file using expat. The code can parse xml correctly in windows.(I test this usign vc6.0), But it cannot parse xml when i run the code in android. I don't know why, the code and the xml file is the same.
For solving this problem, i did some tests:
1. change xml file 's encoding is ANSI: if file doesn't contain chinese, it can be parsed correctly; if not ,parse failed.
2. change xml file's encoding is UTF-8: whatever,the code cannot parse it.
the following is my code :
#include <fstream>
#include <string>
#include <stdlib.h>
#include <stdio.h>
#include <algorithm>
#include "expat.h"
using namespace std;
bool LoadXmlFile(string strfilename);
int Depth;
ize_t offs;
int overflow;
static void
startElement(void *userData, const char *name, const char **atts)
{
puts(name);
int *depthPtr = (int *)userData;
*depthPtr += 1;
}
static void
endElement(void *userData, const char *name)
{
puts(name);
int *depthPtr = (int *)userData;
*depthPtr -= 1;
}
bool LoadXmlFile(string strfilename)
{
XML_Parser parser=XML_ParserCreate(NULL);
if(!parser)
{
printf("Create XML praser FAIL\n");
return false;
}
printf("Enter LoadXmlFile\n");
m_strxmlFileName = strfilename;
long size=0;
FILE* xmlFile=fopen(m_strxmlFileName.c_str(),"rt");
if(NULL==xmlFile)
{
printf("loding file failed\n")
return false;
}
char buf[100];
int done=0;
XML_SetUserData(parser, &Depth);
XML_SetElementHandler(parser, startElement, endElement);
XML_SetCharacterDataHandler(parser,CharHandler);
do
{
memset(buf, 0, sizeof(char) * 100);
int len = (int)fread(buf, 1,sizeof(buf), xmlFile);
done = len < sizeof(buf);
printf("buf first chars :%s\n",buf);
if(!XML_Parse(parser, buf, len, done))
{
done=1;
printf("Parse requestconfig xml FAILED\n");
fclose(xmlFile);
return false;
}
}while(!done);
fclose(xmlFile);
}
int main ()
{
if(!LoadXmlFile("requestconfig.xml"))
printf("parse xml failed");
}
the xml that i use to test is :
<clientrequest requestid="20" desc="中国">
</clientrequest>
thank you very much.