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后程序正常运行,无错误。

开源项目TinyXml项目所涉及的字符编码说明如下: 1. TinyXml函数调用接口的字符型参数,仅支持`窄字符`格式(char*),不兼容`宽字符`格式(wchar_t*)。 2. TinyXml函数提供的Xml内容解析功能,仅支持以ANSI编码和UTF8编码的Xml字符串,也即`多字节编码`。 3. TinyXml函数提供的Xml内容解析功能,不支持内容以UTF16编码和UTF32编码的Xml字符串,也即`Unicode编码`。 4. UTF8编码是Unicode编码的一种实现方式,以不定个数的字节来存储一个Unicode码值,支持多国语言文字。 也即,UTF8编码在编码实现上属于`多字节编码`,在编码标准上属于`Unicode编码`。 5. 人类语言同一个的字符,如果在各种不同的标准如ANSI编码、UTF8编码、UTF16编码和UTF32编码都存在码值, 就可以相互转换。因为各个标准下的可编码字符容量不同,部分语言字符会在一个编码标准库下存在, 而在另外一个编码标准库下不存在,这时精确的相互转换就无法执行,但是仍然有默认字符转换。 -- 6. VC语言,当定义了宏 _UNICODE 后,_T系列宏或函数,以`宽字符`承载UTF16编码。 TinyXml如何提供`宽字符`函数接口,支持解析UTF16编码、UTF32编码标准下的Xml字符串呢? 本项目: - 新增适用于`宽字符`参数的函数接口,调用字符集转换功能,转换为`窄字符`参数,再回调TinyXml原版接口。 - 对于以UTF16编码、或UTF32编码的Xml字符串,转换为UTF8编 --
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值