There are many examples of TinyXml on the Internet. I also need to parse XML recently. I searched for it online and found that TinyXml is the most suitable.
TinyXml supports both Windows and Linux platforms, and is small and comprehensive, including operations on Various XML features.
However, in the United States, although there are many online application instances, most of them parse and save data to files. What I need is to parse the data in the memory and save the data to the memory.
TinyXml online help documentation URL: http://www.grinninglizard.com/tinyxml2docs/index.html
First of all, TinyXml regards the labels, attributes, declarations, comments, and values in Xml data as "nodes", which are embodied in the TiXmlNode data structure.
Because of the restrictions on the conditions, we cannot use test cases now. Let's write down the usage and hope to help you.
Load data from memory:
const char *cszXml = "<?xml version="1.0"?><BOOK><BOOKNAME>XML</BOOKNAME></BOOK>";TiXmlDocument doc;doc.Parse(cszXml);if (true == doc.Error()){cout << "Parse XML data error: " << doc.ErrorDesc() << endl;return;}
Save data to memory:
TinyXml can save data to a file or print data to the screen. Of course, it can also be saved directly to the memory, which is actually the printing function of the application.
Extern TiXmlDocument doc ;//! XML document extern char szXml []; //! Storage result string TiXmlPrinter printer; printer. VisitEnter (doc); // printer. SetIndent ("");//! Set the identifier starting with each line. The default value is 4 spaces // printer. SetLineBreak ("");//! Set the row delimiter. The default value is "\ r \ n "//! The preceding two rows can be used to replace printer. SetStreamPrinting ();//! Set the start identifier to "" and the line separator to "" sprintf (szXml, printer. CStr ());//! Save the result to szXml, a C-style string //! Int nSize = printer. Size ();//! String Length
By the way, the following sentence is provided:
Determine whether a tag has a sub-Tag:
Extern TiXmlElement xmlElem; if (xmlElem. FistChildElement () {// TODO: Sub tag} else {// TODO: No sub tag}
Game over!
It is easy to write, but I hope it will be useful to everyone.