【语言-C++】Xml处理(chenlu1):UTF-8 的中文Key= 中文Value的解析()

来源:互联网 发布:windows 1.0 开源 编辑:程序博客网 时间:2024/05/11 14:40
void LoadXml(){//TCHAR tcBuf[256];//CString str;//::GetModuleFileNameW(GetModuleHandle(NULL),tcBuf,256);//str= tcBuf;//int pos= str.ReverseFind(L'\\');//CString strFilePath  = str.Left(pos+1);//strFilePath = strFilePath + _T("temp\\ss.xml");//char filePath[MAX_PATH];//GetBuffer(strFilePath,filePath);//char filename[512] ="D:\\Work\\DYBit\\Doc\\Materil.xml";TiXmlDocument doc("D:\\Work\\DYBit\\Doc\\Test.xml");if (!doc.LoadFile()) return;TiXmlHandle hDoc(&doc);//TiXmlElement* pElem;TiXmlHandle hRoot(0);const char *tmpChar;TiXmlElement* pElem=hDoc.FirstChildElement().Element();if (!pElem) return ;tmpChar=pElem->Value();hRoot=TiXmlHandle(pElem);TiXmlElement* pMat = hRoot.FirstChild("node").Element();if(!pMat)return ;DWORD dwID=0;for(;pMat;pMat = pMat->NextSiblingElement()){if(strcmp(pMat->Value(),"node")== 0){CString sValue,sKeyName;double MaxValue,MinValue;const char *pKeyName;const char *pValue;double dValue;TiXmlAttribute* pKeyNode;for(pKeyNode = pMat->FirstAttribute();pKeyNode != pMat->LastAttribute();pKeyNode = pKeyNode->Next()){pKeyName = pKeyNode->Name();sKeyName = UTF8toUnicode(pKeyName);if (sKeyName.Compare(_T("等级")) == 0){char pTmp[MAX_PATH];pValue = pKeyNode->Value();sValue = UTF8toUnicode(pValue);dValue =_ttoi(sValue);}if (sKeyName.Compare(_T("昵称")) == 0){char pTmp[MAX_PATH];pValue = pKeyNode->Value();sValue = UTF8toUnicode(pValue);}else if (sKeyName.Compare(_T("生命值")) == 0){char pTmp[MAX_PATH];pValue = pKeyNode->Value();sValue = UTF8toUnicode(pValue);GetBuffer(sValue,pTmp);dValue =atoi(pTmp);//dValue =atof(pTmp);}else if (sKeyName.Compare(_T("魔法值")) == 0){pValue = pKeyNode->Value();sValue = UTF8toUnicode(pValue);int iValue = _ttoi(sValue);}else if (sKeyName.Compare(_T("防御力")) == 0){pValue = pKeyNode->Value();sValue = UTF8toUnicode(pValue);dValue= _ttof(sValue)}else if (sKeyName.Compare(_T("攻击力")) == 0){pValue = pKeyNode->Value();sValue = UTF8toUnicode(pValue);ParseCString(sValue,MaxValue,MinValue);}else if (sKeyName.Compare(_T("攻击范围")) == 0){pValue = pKeyNode->Value();sValue = UTF8toUnicode(pValue);ParseCString(sValue,MaxValue,MinValue);}}}}}void GetBuffer(const CString &strSource,char *pStrTemp){    int pathLength=strSource.GetLength();    for(int i=0; i<pathLength; i++)    {       * pStrTemp=strSource.GetAt(i);        pStrTemp++;    }     * pStrTemp='\0';}void ParseCString(const CString &str,double &MaxValue,double &MinValue){char pStrTemp1[MAX_PATH];char pStrTemp2[MAX_PATH];int pathLength=str.GetLength();int iTmp = 0 ;double dTmp1 = 0.0,dTmp2 = 0.0;BOOL OneStr = TRUE;int Type  = 0 ;for(int i=0; i<pathLength; i++)    {       CString sTmp;sTmp = str.GetAt(i);if (sTmp.Compare(_T("~"))==0 ){pStrTemp1[i] = '\0'; OneStr = FALSE;Type = 1;iTmp = i ; continue;}else if (sTmp.Compare(_T("±")) == 0){pStrTemp1[i] = '\0'; OneStr =FALSE;Type = 2;iTmp = i; continue;}if (OneStr){ pStrTemp1[i]=str.GetAt(i); }else{pStrTemp2[i-iTmp-1]=str.GetAt(i); }}  dTmp1 = atof(pStrTemp1);dTmp2 = atof(pStrTemp2);if (Type == 1){MaxValue = dTmp2;MinValue = dTmp1;}else if (Type == 2){MaxValue = dTmp1 + dTmp2;MinValue = dTmp1 - dTmp2;}else{MaxValue = MinValue =  dTmp1;}}CString UTF8toUnicode(const char* utf8Str,UINT length){CString unicodeStr;unicodeStr=_T("");if (!utf8Str)return unicodeStr;if (length==0)return unicodeStr;WCHAR chr=0;for (UINT i=0;i<length;){if ((0x80&utf8Str[i])==0) {chr=utf8Str[i];i++;}else if((0xE0&utf8Str[i])==0xC0) {chr =(utf8Str[i+0]&0x3F)<<6;chr|=(utf8Str[i+1]&0x3F);i+=2;}else if((0xF0&utf8Str[i])==0xE0){chr =(utf8Str[i+0]&0x1F)<<12;chr|=(utf8Str[i+1]&0x3F)<<6;chr|=(utf8Str[i+2]&0x3F);i+=3;}else {return unicodeStr;}unicodeStr.AppendChar(chr);}return unicodeStr;}CString UTF8toUnicode(const char* utf8Str){UINT theLength=strlen(utf8Str);return UTF8toUnicode(utf8Str,theLength);}

Test.xml

<?xml version="1.0" encoding="utf-8" ?><DnfInfo><node 等级="1" 昵称="使命召唤" 生命值="125" 魔法值="120" 防御力="7" 攻击力="13~20" 攻击范围="10±10"  /><node 等级="10" 昵称="极度剑客" 生命值="890" 魔法值="800" 防御力="120" 攻击力="97~125" 攻击范围="30±30"  /><node 等级="20" 昵称="生死亡徒" 生命值="2300" 魔法值="2100" 防御力="1000" 攻击力="200~237" 攻击范围="50±50"  /><node 等级="30" 昵称="灭顶神使" 生命值="3380" 魔法值="3200" 防御力="3000" 攻击力="300~456" 攻击范围="70±70"   /><node 等级="40" 昵称="再造幻影" 生命值="5270" 魔法值="4800" 防御力="7000" 攻击力="960~1045" 攻击范围="90±90"   /><node 等级="50" 昵称="断残殇" 生命值="9230" 魔法值="7200" 防御力="13000" 攻击力="2600~2800" 攻击范围="110±110"  /><node 等级="60" 昵称="恶灵之咒" 生命值="14900" 魔法值="11000" 防御力="20000" 攻击力="3200~3589" 攻击范围="130±130"  /><node 等级="70" 昵称="迷途" 生命值="21200" 魔法值="16000" 防御力="28800" 攻击力="4100~6000" 攻击范围="150±150"  /><node 等级="80" 昵称="龙蛇狂舞" 生命值="39100" 魔法值="20000" 防御力="39900" 攻击力="5100~7800" 攻击范围="170±170"  /><node 等级="85" 昵称="孤心流浪" 生命值="45100" 魔法值="25600" 防御力="50000" 攻击力="5900~8500" 攻击范围="180±180"   /></DnfInfo>


原创粉丝点击