用CMarkup类,读入XML文件,读入的中文为乱码,请问怎样解决?

laohubinbin 2007-05-08 09:04:54
谢谢
...全文
665 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
yqmfly 2011-08-31
  • 打赏
  • 举报
回复
看看这篇文章的方法是否对你有用,我曾经用这个方法解决过。
http://blog.163.com/lvan100@yeah/blog/static/6811721420115134953280/
还有这个方法,http://zhanyonhu.blog.163.com/blog/static/16186044200921742521746/
ToperRay 2007-05-09
  • 打赏
  • 举报
回复
bool Load( LPCTSTR szFileName );

你用这个函数

或者

改这块

// Windows Unicode file is detected if starts with FEFF
if ( pBuffer[0] == 0xff && pBuffer[1] == 0xfe )
{
// Contains byte order mark, so assume wide char content
// non _UNICODE builds should perform UCS-2 (wide char) to UTF-8 conversion here
csText = (LPCWSTR)(&pBuffer[2]);
csNotes += _T("File starts with hex FFFE, assumed to be wide char format. ");
}
else
{
// _UNICODE builds should perform UTF-8 to UCS-2 (wide char) conversion here
csText = (LPCSTR)pBuffer;
}

这里,UTF-8的解码过程是错误的
paradise099 2007-05-08
  • 打赏
  • 举报
回复
你看看你的XML文件的编码方式,以及你文件存储的方式是否正确。
paradise099 2007-05-08
  • 打赏
  • 举报
回复
这个问题我也遇到过,你看看
蒋晟 2007-05-08
  • 打赏
  • 举报
回复
不要一贴多发
laohubinbin 2007-05-08
  • 打赏
  • 举报
回复
我是这样读取的:
CString csFilename =_T("information.xml") ;

CString csText;
CString csNotes;
CFile file;
if ( ! file.Open( csFilename, CFile::modeRead ) )
{
AfxMessageBox(_T("unable to open file"));
return;
}
int nFileLen = (int)file.GetLength();

// Allocate buffer for binary file data
unsigned char* pBuffer = new unsigned char[nFileLen + 2];
nFileLen = file.Read( pBuffer, nFileLen );
file.Close();
pBuffer[nFileLen] = '\0';
pBuffer[nFileLen+1] = '\0'; // in case 2-byte encoded

// Windows Unicode file is detected if starts with FEFF
if ( pBuffer[0] == 0xff && pBuffer[1] == 0xfe )
{
// Contains byte order mark, so assume wide char content
// non _UNICODE builds should perform UCS-2 (wide char) to UTF-8 conversion here
csText = (LPCWSTR)(&pBuffer[2]);
csNotes += _T("File starts with hex FFFE, assumed to be wide char format. ");
}
else
{
// _UNICODE builds should perform UTF-8 to UCS-2 (wide char) conversion here
csText = (LPCSTR)pBuffer;
}
delete [] pBuffer;

// If it is too short, assume it got truncated due to non-text content
if ( csText.GetLength() < nFileLen / 2 - 20 )
{
AfxMessageBox( _T("Error converting file to string (may contain binary data)") );
return;
}

// Parse
CMarkup xml;
BOOL bResult = xml.SetDoc( csText );
while ( xml.FindChildElem(_T("DrawingInfo")) )
{
xml.IntoElem();
while (xml.FindChildElem( _T("ID") ))
{
xml.IntoElem();
CString tmpStr;
tmpStr=xml.GetAttrib(_T("DrawingID")) ;
m_myDialogBarLeft.SetDlgItemText(IDC_STATIC_DRAWING_ID, tmpStr);
xml.OutOfElem();
}
while (xml.FindChildElem( _T("Name") ))
{
xml.IntoElem();
CString tmpStr;
tmpStr=xml.GetAttrib(_T("DrawingName"));
m_myDialogBarLeft.SetDlgItemText(IDC_STATIC_DRAWING_NAME, tmpStr);
xml.OutOfElem();
}
xml.OutOfElem();
}

当读到“我爱祖国”时就为乱码了,tmpStr显示为“磽 b蚖羱'hWm”的乱码
laohubinbin 2007-05-08
  • 打赏
  • 举报
回复
就是这个文件:
<?xml version='1.0' encoding='UTF-8' standalone='no'?><drawinginformation version="1.0">
<OriginalInfo>
<ID ProcessID="1001"/>
</OriginalInfo>
<DrawingInfo>
<ID DrawingID="00175773"/>
<Name DrawingName="我爱祖国"/>
<Address DrawingAddress="23"/>
</DrawingInfo>
<HouseInfo>
<ID HouseID="0123"/>
<Name HouseName="405号"/>
<Direction HouseDirection="西南"/>
<Code HuxingCode="24"/>
<Name HuxingName="2LDK"/>
<Code HuxingTumianCode="230123456789"/>
<erea HuxingTumianerea="60.00"/>
<tieshu HuxingTumiantieshu="18.15"/>
</HouseInfo>
<Operator>
<Code OperatorCode="0102"/>
<Name OperatorName="某人"/>
</Operator>
<XMLData>
<path path="\\192.168.1.159\d:\间取\间取图xml"/>
<Name FileName="0"/>
</XMLData>
<XMLInfo>
<path path="\c:\间取\间取情报xml"/>
<Name FileName="info_in.xml"/>
</XMLInfo>
<JPGNewFile>
<path path="\\192.168.1.159\画像\公开画像\23"/>
<FileName File1Name="0"/>
</JPGNewFile>
<EPSFileWin>
<path path="\\192.168.1.159\画像\印刷画像\epswin"/>
<FileName FileName="0"/>
</EPSFileWin>
<EPSFileMac>
<path path="\\192.168.1.159\画像\印刷画像\epsmac"/>
<FileName FileName="0"/>
</EPSFileMac>
<PHOTOFile>
<path path="\\192.168.1.159\室内写真"/>
<FileName FileName="0"/>
</PHOTOFile>
<JPGOldFile>
<path path="\\192.168.1.159\画像\公开画像\23"/>
<FileName File1Name="230123456789.jpg"/>
</JPGOldFile>
</drawinginformation>

3,055

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC HTML/XML
社区管理员
  • HTML/XML社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧