In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-05-02 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly introduces "how C++ uses TinyXML to parse XML". In daily operation, I believe many people have doubts about how C++ uses TinyXML to parse XML. The editor consulted all kinds of materials and sorted out simple and easy-to-use methods of operation. I hope it will be helpful to answer the doubts about "how C++ uses TinyXML to analyze XML". Next, please follow the editor to study!
1. Introduction
The official website of Tinyxml: http://www.grinninglizard.com
Official introduction document: http://www.grinninglizard.com/tinyxmldocs/tutorial0.html
In TinyXML, some classes are defined based on the various elements of XML:
TiXmlBase: the base class of the entire TinyXML model.
TiXmlAttribute: attributes that correspond to elements in XML.
TiXmlNode: corresponds to a node in the DOM structure.
TiXmlComment: corresponds to comments in XML
TiXmlDeclaration: corresponds to the declaration section of the XML, that is.
TiXmlDocument: the entire document corresponding to XML.
TiXmlElement: the element corresponding to XML.
TiXmlText: the text portion corresponding to XML
TiXmlUnknown: corresponds to the unknown part of the XML.
TiXmlHandler: defines some operations for XML.
Use the following figure to illustrate the text format for commonly used classes:
/ / TiXmlDeclaration, declare / / TiXmlElement, element / / TiXmlComment, comment / / TiXmlElement, element Welcome to MyApp// is element TiXmlElement, "Welcome to MyApp" is TiXmlText, text Thank you for using MyApp// is the same as above / / TiXmlElement, element / / Window is element TiXmlElement, name, x, y, h are TiXmlAttribute
TinyXML is a parsing library, which is mainly composed of DOM model classes (TiXmlBase, TiXmlNode, TiXmlAttribute, TiXmlComment, TiXmlDeclaration, TiXmlElement, TiXmlText, TiXmlUnknown) and TiXmlHandler classes. It consists of two header files (.h file) and four CPP files (.cpp file). When you use it, you can use it as long as you import (tinyxml.h, tinystr.h, tinystr.cpp, tinyxml.cpp, tinyxmlerror.cpp, tinyxmlparser.cpp) into the project. If necessary, you can make it your own DLL to call.
Note that TiXmlBase is the base class for TiXmlNode, and TiXmlNode is the base class for TiXmlElement, TiXmlComment, TiXmlText, TiXmlDeclaration, TiXmlUnknown, and TiXmlDocument.
2.TinyXML configuration
Add the header file reference # include "tinyxml/tinyxml.h" to the stdafxh header file
Add lib reference library to the project settings
Add a dynamic library reference to stdafx.h
# ifdef _ DEBUG#pragma comment (lib, "TinyXMLD.lib") # else#pragma comment (lib, "TinyXML.lib") # endif3.TinyXML read and save file 3.1 read xml file TiXmlDocument lconfigXML;if (! lconfigXML.LoadFile (strXmlFile.c_str ())) {break;} 3.2 read xml parameter TiXmlDocument lActionXML;lActionXML.Parse (strRmcpParam.c_str ()); if (lActionXML.Error ()) {strErr = "input parameter is not in standard xml format" Return false;} 3.3Save xml parameters to the text TiXmlDocument tyDoc;. TyDoc.SaveFile (m_strFilePath); 3.4 saves the xml parameter to the temporary variable TiXmlDocument tyDoc;. TiXmlPrinter printer;tyDoc.Accept (& printer); std::string devParam = std::string (printer.CStr ()); 4.TinyXML addition, deletion, modification and search 4.1 increase
Create a xml file code such as in 1
Void write_app_settings_doc () {TiXmlDocument doc; TiXmlElement* msg; TiXmlDeclaration* decl = new TiXmlDeclaration; doc.LinkEndChild (decl); TiXmlElement* root = new TiXmlElement ("MyApp"); doc.LinkEndChild (root); TiXmlComment * comment = new TiXmlComment (); comment- > SetValue ("Settings for MyApp"); root- > LinkEndChild (comment) TiXmlElement * msgs = new TiXmlElement ("Messages"); root- > LinkEndChild (msgs); msg = new TiXmlElement ("Welcome"); msg- > LinkEndChild ("Welcome to MyApp"); msgs- > LinkEndChild (msg); msg = new TiXmlElement ("Farewell"); msg- > LinkEndChild (new TiXmlText ("Thank you for using MyApp")); msgs- > LinkEndChild (msg) TiXmlElement * windows = new TiXmlElement ("Windows"); root- > LinkEndChild (windows); TiXmlElement * window; window = new TiXmlElement ("Window"); windows- > LinkEndChild (window); window- > SetAttribute ("name", "MainFrame"); window- > SetAttribute ("x", 5); window- > SetAttribute ("y", 15); window- > SetAttribute ("w", 400); window- > SetAttribute ("h", 250) TiXmlElement * cxn = new TiXmlElement ("Connection"); root- > LinkEndChild (cxn); cxn- > SetAttribute ("ip", "192.168.0.1"); cxn- > SetDoubleAttribute ("timeout", 123.456); / / floating point attrib dump_to_stdout (& doc); doc.SaveFile ("appsettings.xml");}
Insert a new node at the end of the node
TiXmlNode* LinkEndChild (TiXmlNode* addThis)
Insert a new node before / after the node
TiXmlNode* InsertBeforeChild (TiXmlNode* beforeThis, const TiXmlNode& addThis); TiXmlNode* InsertAfterChild (TiXmlNode* afterThis, const TiXmlNode& addThis); 4.2 Delete
Delete a node. TiXmlNode is the base class of TiXmlElement, TiXmlComment, TiXmlText, TiXmlDeclaration, TiXmlUnknown, TiXmlDocument
TiXmlNode node;node.Clear ()
Remove child node B from node A
TiXmlNode nodeA;nodeA. RemoveChild (TiXmlNode* removeThis)
Remove an attribute named B from element A
TiXmlAttribute attrA;attrA. RemoveAttribute (const char * name)
The search is, now you need to change 1234 to a different value.
TiXmlNode* lpnode = NULL;lpnode = tixml.RootElement ()-> IterateChildren ("mfid", lpnode); TiXmlAttribute* tiattr = lpnode- > ToElement ()-> FirstAttribute (); / / find the mfid node and get the first attribute value. Note that if there are multiple attribute values, you need to determine which attribute value is the required tiattr- > SetValue (mfid.c_str ())
Replace a node
TiXmlNode* ReplaceChild (TiXmlNode* replaceThis, const TiXmlNode& withThis); 4.4 check
Get the link node
Const TiXmlNode* lpItemNode = NULL;// initialization lpItemNode = lconfigXML.RootElement ()-> IterateChildren ("link", lpItemNode); if (lpItemNode = = NULL) {/ / Can not find break;}
Get the value of the type attribute in the link node
Std::string strType = lpItemNode- > ToElement ()-> Attribute ("type")
Ergodic node
Const TiXmlNode* lpMapNode = NULL; / / initialize lpMapNode = lconfigXML.RootElement ()-> IterateChildren ("node", lpMapNode); if (lpMapNode) {rms::CStationMapping litem; const TiXmlNode* lpItemNode = NULL; while (lpItemNode = lpMapNode- > IterateChildren ("item", lpItemNode)) {string str = lpItemNode- > ToElement ()-> Attribute ("ABC");}}
Traversing element attributes
TiXmlAttribute* pAttr = NULL; for (pAttr = pNode- > FirstAttribute (); pAttr; pAttr = pAttr- > Next ()) {... }
The next sibling node of the node
Const TiXmlNode* NextSibling () const
Next element of the element
Const TiXmlElement* NextSiblingElement () const
The next property of the property
Const TiXmlAttribute* Next () const
The return value of NULL indicates that it does not exist.
5. A complete example
Void AppSettings::load (const char* pFilename) {TiXmlDocument doc (pFilename); if (! doc.LoadFile ()) return; TiXmlHandle hDoc (& doc); TiXmlElement* pElem; TiXmlHandle hRoot (0); / / block: name {pElem=hDoc.FirstChildElement (). Element (); / / should always have a valid root but handle gracefully if it does if (! pElem) return; / / save this for later hRoot=TiXmlHandle (pElem);} / / block: string table {m_messages.clear (); / / trash existing table pElem=hRoot.FirstChild ("Messages"). FirstChild (). Element (); for (pElem; pElem; pElem=pElem- > NextSiblingElement ()) {const char * pKey=pElem- > Value (); const char * pText=pElem- > GetText () If (pKey & & pText) {m _ messages [PKey] = pText;}} / / block: windows {m_windows.clear (); / / trash existing list TiXmlElement* pWindowNode=hRoot.FirstChild ("Windows"). FirstChild (). Element (); for (pWindowNode; pWindowNode) PWindowNode=pWindowNode- > NextSiblingElement () {WindowSettings w; const char * pName=pWindowNode- > Attribute ("name"); if (pName) W. namename; pWindowNode- > QueryIntAttribute ("x", & W.X); / / If this fails, original value is left as-is pWindowNode- > QueryIntAttribute ("y", & W.Y) PWindowNode- > QueryIntAttribute ("w", & W.W); pWindowNode- > QueryIntAttribute ("hh", & W.H); m_windows.push_back (w);} / / block: connection {pElem=hRoot.FirstChild ("Connection"). Element (); if (pElem) {missuconnection.iproompElm-> Attribute ("ip") PElem- > QueryDoubleAttribute ("timeout", & m_connection.timeout);} this ends the study of "how C++ uses TinyXML to parse XML". I hope I can solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!
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: 242
*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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.