您好,登錄后才能下訂單哦!
本篇文章給大家分享的是有關如何用tinyxml 庫創建并讀寫xml代碼截取,小編覺得挺實用的,因此分享給大家學習,希望大家閱讀完這篇文章后可以有所收獲,話不多說,跟著小編一起來看看吧。
因為自己每次寫完之后都忘記了 然后又要從別的地方學習再重新寫 還不如記錄再這里 //創建 TiXmlDocument *pXmlDocument = new TiXmlDocument(m_strFilePath.c_str());// TiXmlDeclaration *pDeclaretion = new TiXmlDeclaration("1.0", "UTF-8","");//創建xml聲明 pXmlDocument->LinkEndChild(pDeclaretion); TiXmlElement *pXmlElement = new TiXmlElement("Symbol"); pXmlDocument->LinkEndChild(pXmlElement); pXmlDocument->SaveFile(m_strFilePath.c_str()); //讀 TiXmlDocument *pXmlDoc = new TiXmlDocument(); pXmlDoc->LoadFile(strFilePath.c_str()); TiXmlElement *pRootElement = pXmlDoc->RootElement(); if (pRootElement == NULL) { return; } // 第一個子節點 TiXmlElement *pSignNode = pRootElement->FirstChildElement("sign"); if (!pSignNode) { SAFE_DELETE(pSignNode); return; } while (pSignNode != NULL) { CMapSingleParameter *pMapSingleParameter = new CMapSingleParameter(); if (!pMapSingleParameter) { SAFE_DELETE(pMapSingleParameter); return ; } // text TCHAR tcharValue[MAX_PATH * 4] = {_T('\0')}; // name const CHAR *pszName = pSignNode->Attribute("name"); if (pszName != NULL) { CommonUtil::UTF8ToTCHAR(pszName,tcharValue); pMapSingleParameter->AppendParameter(_T("name"), tcharValue); } // height const CHAR *pszHeight = pSignNode->Attribute("height"); if (pszHeight != NULL) { CommonUtil::UTF8ToTCHAR(pszHeight,tcharValue); pMapSingleParameter->AppendParameter(_T("height"), tcharValue); } //Content const CHAR *pszContent = pSignNode->GetText(); if (pszContent != NULL) { CommonUtil::UTF8ToTCHAR(pszContent,tcharValue); pMapSingleParameter->AppendParameter(_T("content"), tcharValue); } m_pLatelyVectorSymbol->push_back(pMapSingleParameter); pSignNode = pSignNode->NextSiblingElement(); } //寫 string strFile = m_strFilePath; //創建文檔對象 TiXmlDocument myXmlDocument; //加載文件數據 myXmlDocument.LoadFile(strFile.c_str()); TiXmlElement *pRootElement = myXmlDocument.RootElement(); if (pRootElement == NULL) { return ; } if (pRootElement != NULL) { TiXmlElement *pFirstNode = pRootElement->FirstChildElement("sign"); if (pFirstNode == NULL) { int nSignPos = 0; for (TVectorSymbol::iterator ite = m_pLatelyVectorSymbol->begin(); ite != m_pLatelyVectorSymbol->end(); ++ite) { CMapSingleParameter *pMapSingleParameter = *ite; TiXmlElement *insertElement = new TiXmlElement("sign"); pRootElement->LinkEndChild(insertElement); xstring strUnicode = pMapSingleParameter->GetParameterValue(_T("name")); TCHAR tcharValue[MAX_PATH*4]= {_T('\0')}; _tcscpy(tcharValue, strUnicode.c_str()); char chUtf8[MAX_PATH*4] = {'\0'}; CommonUtil::TCHARToUTF8(tcharValue, chUtf8); insertElement->SetAttribute("name",chUtf8); strUnicode = pMapSingleParameter->GetParameterValue(_T("height")); _tcscpy(tcharValue, strUnicode.c_str()); CommonUtil::TCHARToUTF8(tcharValue, chUtf8); insertElement->SetAttribute("height",chUtf8); strUnicode = pMapSingleParameter->GetParameterValue(_T("content")); _tcscpy(tcharValue, strUnicode.c_str()); CommonUtil::TCHARToUTF8(tcharValue, chUtf8); TiXmlText *pXmlText = new TiXmlText(chUtf8); pXmlText->SetCDATA(true);//添加格式化<![CDATA[]]> insertElement->LinkEndChild(pXmlText); m_nLatelyTotal ++; } } } myXmlDocument.SaveFile(strFile.c_str()); } //刪除節點 string strFile = m_strFilePath; //創建文檔對象 TiXmlDocument myXmlDocument; //加載文件數據 myXmlDocument.LoadFile(strFile.c_str()); TiXmlElement *pRootElement = myXmlDocument.RootElement(); if (pRootElement == NULL) { return ; } TiXmlElement *pSignNode = pRootElement->FirstChildElement("sign"); while (pSignNode != NULL) { m_nLatelyTotal --; TiXmlElement *pSignRemoveNode = pSignNode; pSignNode= pSignNode->NextSiblingElement("sign"); pRootElement->RemoveChild(pSignRemoveNode); } myXmlDocument.SaveFile(strFile.c_str());
以上就是如何用tinyxml 庫創建并讀寫xml代碼截取,小編相信有部分知識點可能是我們日常工作會見到或用到的。希望你能通過這篇文章學到更多知識。更多詳情敬請關注億速云行業資訊頻道。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。