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)多字节转成宽字节
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后程序正常运行,无错误。