当我从我的 XML 文档中打印出信息时,我会在每一行之前得到 ��。这是我的 XML 文档。
<?xml version="1.0" encoding="ISO-8859-1"?>
<note>
<to>Tove</to>
<from>Jani</from>
<heading>Reminder</heading>
<body>Don't forget me this weekend!</body>
</note>
这是我的代码
#include <cstdio>
#include <expat.h>
#include <string>
#include <fstream>
#include <cstring>
using namespace std;
void XMLCALL start(void*,const char*, const char**);
void XMLCALL end(void*,const char*);
void XMLCALL character(void*, const char*, int);
int main()
{
int size;
fstream fin("note.xml", ios::in | ios::binary | ios::ate);
size = fin.tellg();
char* data = new char [size + 1];
fin.seekg(0, ios::beg);
XML_Parser parser = XML_ParserCreate("ISO-8859-1");
XML_SetElementHandler(parser, start, end);
XML_SetCharacterDataHandler(parser, character);
fin.read(data, size);
XML_Parse(parser, data, size , fin.eof());
XML_ParserFree(parser);
fin.close();
delete [] data;
}
void XMLCALL start(void* data, const char* name, const char** atts)
{
if((strcmp(name, "note")) == 0)
{
printf("-- Note --<br> ");
}
else if((strcmp(name,"to")) == 0)
{
printf("To: ");
}
else if((strcmp(name,"from")) == 0)
{
printf("From: ");
}
else if((strcmp(name,"heading")) == 0)
{
printf("Heading: ");
}
else if((strcmp(name, "body")) == 0)
{
printf("Message: ");
}
}
void XMLCALL end(void* data,const char* name)
{
printf("<br>");
}
void XMLCALL character(void* data, const char* info, int length)
{
printf("%s",info);
}
这是它打印出来的:
-- Note --<br>
�To: Tove<br>
xFrom: Jani<br>
xHeading: Reminder<br>
xMessage: Don't forget me this weekend!<br>
x<br>
这就是我希望它打印出来的内容:
-- Note --<br>
To: Tove<br>
From: Jani<br>
Heading: Reminder<br>
Message: Don't forget me this weekend!<br>
<br>
任何帮助将不胜感激。谢谢你