Get the App
SLTechnology News&Howtos  ›  Development  › 

How to create, read and write xml code interception with tinyxml library

Shulou Source: shulou.com Published: 2022-06-03 05:54:44 10月05日 Update

This article is about how to use the tinyxml library to create and read and write xml code interception, the editor feels very practical, so share with you to learn, I hope you can get something after reading this article, say no more, follow the editor to have a look.

Because I forget every time I finish writing, and then I have to learn from somewhere else to rewrite. It would be better to record here / / create TiXmlDocument * pXmlDocument = new TiXmlDocument (m_strFilePath.c_str ()); / / TiXmlDeclaration * pDeclaretion = new TiXmlDeclaration ("1.0", "UTF-8", ""); / / create xml statement pXmlDocument- > LinkEndChild (pDeclaretion); TiXmlElement * pXmlElement = new TiXmlElement ("Symbol") PXmlDocument- > LinkEndChild (pXmlElement); pXmlDocument- > SaveFile (m_strFilePath.c_str ()); / / read TiXmlDocument * pXmlDoc = new TiXmlDocument (); pXmlDoc- > LoadFile (strFilePath.c_str ()); TiXmlElement * pRootElement = pXmlDoc- > RootElement (); if (pRootElement = = NULL) {return;} / / first child node 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) } mroompLatelyVectorSymbola-> push_back (pMapSingleParameter); pSignNode = pSignNode- > NextSiblingElement ();} / / write string strFile = massistrFilePath; / / create document object TiXmlDocument myXmlDocument; / / load file data 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 = massipLatelyVectorSymbo-> begin (); ite! = massipLatelyVectorSymbolo-> 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) / / add formatted insertElement- > LinkEndChild (pXmlText); m_nLatelyTotal + +;} myXmlDocument.SaveFile (strFile.c_str ());} / / Delete node string strFile = massistrFilePath; / / create document object TiXmlDocument myXmlDocument / / load file data 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 ()); the above is how to use the tinyxml library to create and read and write xml code interception. The editor believes that there are some knowledge points that we may see or use in our daily work. I hope you can learn more from this article. For more details, please follow the industry information channel.

Tags: Code objects data files documents more knowledge articles nodes learning utility size place that is work meetings articles formats see knowledge points industry Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno OPPO Reno Xiaomi Shulou Tech Info macOS vpn