Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

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

2025-04-02 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

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.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report