tinyxml在MFC、UNICODE中的应用

1、下载tinyxml,下载地址:http://sourceforge.net/projects/tinyxml/

2、把tinyxml工程中的tinyxml.h,tinyxml.cpp,tinystr.h,tinystr.cpp,tinyxmlerror.cpp,tinyxmlparser.cpp 文件拷贝加入到自己的工程中。

3、在.cpp中第1行加入#include "stdafx.h"。

4、写XML

void CDlgSCR::saveInfoToXml(CString pFilename,CString strFname,int iPlayTime)
{
 string strFileName = theApp.converToMultiChar(( LPCWSTR )pFilename);
 TiXmlDocument doc;
 if (!doc.LoadFile(strFileName.c_str()))
 {
  TiXmlDeclaration* decl = new TiXmlDeclaration( "1.0", "gb2312", "" ); 
  doc.LinkEndChild( decl );

  string strTemp;
  string strRoot;
  CString strRootName = theApp.m_strTerminalName + _T("-FlashList");
  CString strDateTime = _T("");
  CString strSend = strFname;
  CString strInfo = _T("Flash");
  CTime time = CTime::GetCurrentTime();
  strDateTime = time.Format(_T("%Y-%m-%d %H:%M:%S"));
  strTemp = theApp.converToMultiChar(( LPCWSTR )strSend);

  strRoot = theApp.converToMultiChar(( LPCWSTR )strRootName);
  TiXmlElement* root = new TiXmlElement(strRoot.c_str());
  doc.LinkEndChild(root);

  TiXmlElement * msgs = new TiXmlElement( strTemp.c_str() );
  root->LinkEndChild(msgs);

  TiXmlElement * flashInfo;
  strTemp = theApp.converToMultiChar((LPCWSTR)strInfo);
  flashInfo = new TiXmlElement(strTemp.c_str());
  msgs->LinkEndChild(flashInfo);
  flashInfo->SetAttribute("PlayTime",iPlayTime);
  strTemp = theApp.converToMultiChar(( LPCWSTR )strDateTime);
  flashInfo->SetAttribute("PlayDateTime",strTemp.c_str());

  doc.SaveFile(strFileName.c_str());
 }
 else
 {
  TiXmlHandle hDoc(&doc);
  TiXmlElement* pElem;
  TiXmlHandle hRoot(0);
  pElem = hDoc.FirstChildElement().Element();
  string strTemp;
  string sFname;
  sFname = theApp.converToMultiChar((LPCWSTR)strFname);
  CString strInfo = _T("Flash");
  CString strDateTime = _T("");
  CTime time = CTime::GetCurrentTime();
  strDateTime = time.Format(_T("%Y-%m-%d %H:%M:%S"));
  hRoot = TiXmlHandle(pElem);

  pElem = hRoot.FirstChild(sFname.c_str()).Element();
  if (!pElem)
  {
   TiXmlElement * msgs = new TiXmlElement(sFname.c_str());
   hDoc.FirstChildElement().Element()->LinkEndChild(msgs);

   TiXmlElement * flashInfo;
   strTemp = theApp.converToMultiChar((LPCWSTR)strInfo);
   flashInfo = new TiXmlElement(strTemp.c_str());
   msgs->LinkEndChild(flashInfo);
   flashInfo->SetAttribute("PlayTime",iPlayTime);
   strTemp = theApp.converToMultiChar(( LPCWSTR )strDateTime);
   flashInfo->SetAttribute("PlayDateTime",strTemp.c_str());
  }
  else
  {
   TiXmlElement * flashInfo;
   strTemp = theApp.converToMultiChar((LPCWSTR)strInfo);
   flashInfo = new TiXmlElement(strTemp.c_str());
   pElem->LinkEndChild(flashInfo);
   flashInfo->SetAttribute("PlayTime",iPlayTime);
   strTemp = theApp.converToMultiChar(( LPCWSTR )strDateTime);
   flashInfo->SetAttribute("PlayDateTime",strTemp.c_str());
  }

  doc.SaveFile(strFileName.c_str());
 }
}

5、由于tinyxml中用到的是const char *,而MFC中UNICODE下用的多是CString,因此需要进行转换。

(1)多字节转成宽字节

wstring xx::converToWideChar( const string& str )
{
              int  len = 0;
 
              len = str.length();
 
              int  unicodeLen = ::MultiByteToWideChar(CP_UTF8,0,str.c_str(),-1,NULL,0); 
 
              wchar_t *  pUnicode; 
              pUnicode = new  wchar_t[unicodeLen+1]; 
 
              memset(pUnicode,0,(unicodeLen+1)*sizeof(wchar_t)); 
 
              ::MultiByteToWideChar(CP_UTF8,0,str.c_str(),-1,(LPWSTR)pUnicode,unicodeLen); 
 
              wstring  rt; 
              rt = ( wchar_t* )pUnicode;
              delete  pUnicode;
             
              return  rt; 
}
使用时:

String input;

              wstring wstrInput;

              wstrInput = converToWideChar( input );

              CString strText;

              strText.Format( _T( “%s” ), wstrInput.c_str() );

(2)宽字节转成多字节

string BasicUtility::converToMultiChar( const wstring& str )

{

              char*     pElementText;

              int                           iTextLen;

              // wide char to multi char

              iTextLen = WideCharToMultiByte( CP_ACP,

                                                                                                                              0,

                                                                                                                              str.c_str(),

                                                                                                                              -1,

                                                                                                                              NULL,

                                                                                                                              0,

                                                                                                                              NULL,

                                                                                                                              NULL );

              pElementText = new char[iTextLen + 1];

              memset( ( void* )pElementText, 0, sizeof( char ) * ( iTextLen + 1 ) );

              ::WideCharToMultiByte( CP_ACP,

                                                                                       0,

                                                                                       str.c_str(),

                                                                                       -1,

                                                                                       pElementText,

                                                                                       iTextLen,

                                                                                       NULL,

                                                                                       NULL );

              string strText;

              strText = pElementText;

              delete[] pElementText;

             return strText;

}

使用时:

              CString strPdataFile;

              string strKbFile;

              strKbFile =converToMultiChar( ( LPCWSTR )strPdataFile );

由于本人使用中包含中文,因此在上面的应用中编译时会出现错误,把CP_UTF8换成CP_ACP后程序正常运行,无错误。

  • 0
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值