To write an XML file, tinyxml is a mini C ++ XML parsing library, which is not verified and can be easily integrated into other programs. it parses an XML Doc to create a Dom that can be read, written, and saved. its main class level architecture, function prototype details see: http://www.grinninglizard.com/tinyxml/index.html
// The following uses the simple program tinyxmltest as an example. The most fundamental part of tinyxml is document. Therefore, whether you need to write an XML file or read it, you must start with a document, that is, (1) here, we didn't give the constructor a parameter because our purpose is to write an XML file, and the file name is the parameter passed to tixmldocument. Declarations, documents, comments, text, elements, and unknown types are all subclasses of tinyxmlnode, all of which are a node. tinyxmlnode is a very complicated thing, as shown above:
# Include
# Include "tinyxml. H"
Using namespace STD;
Int main (INT argc, char ** argv)
{
// (1) create a XML document
Tixmldocument * mydoc = new tixmldocument ();
After a document node is created, you need to give the DOM tree structure a root, that is, the following (2), (3), (5), because tinyxml is not verified, therefore, in theory, he can have two documents (to avoid this situation by judging the types internally). Because the root is an element, and the element is essentially a container, it can have sub-elements, text, etc. Because there can be multiple attributes, it has a member of tinyxmlattrbuiteset internally for storage, the attrbuites stored in tinyxmlattrbuiteset are implemented through a linked list with a "Sentinel. each link is placed at the last position.
// (2) create the Root and connect it
TiXmlElement * RootElement = new TiXmlElement ("Staff Group ");
MyDoc-> LinkEndChild (RootElement );
// (3) create a person and connect it
TiXmlElement * PersonElement = new TiXmlElement ("Personnel ");
RootElement-> LinkEndChild (PersonElement );
As shown in (4), if an Element has attributes that need to be set, you can call the SetAttribute () method to achieve the goal. During the setting, the object will be searched on the internal linked list of the Element, if the corresponding attribute name already exists, it is regarded as "Rewrite". If not, add it.
// (4) set the attribute fo Person
PersonElement-> SetAttribute ("ID", "1 ");
// (5) create Elementy name & age and connect them
TiXmlElement * NameElement = new TiXmlElement ("name ");
TiXmlElement * AgeElement = new TiXmlElement ("Age ");
PersonElement-> LinkEndChild (NameElement );
PersonElement-> LinkEndChild (AgeElement );
As shown in figure 6, text should be the best tag type to be processed in XML. It can only exist as "leaves" in the DOM tree structure.
// (6) set element name & age and connect it
Tixmltext * namecontent = new tixmltext (" ");
Tixmltext * agecontent = new tixmltext ("20 ");
Nameelement-> linkendchild (namecontent );
Ageelement-> linkendchild (agecontent );
The most troublesome thing is (7). First, open star in the "w" mode. XML file (if it does not exist, it is created). After some preprocessing in the SaveFile, it enters a print method inherited from tinyxmlbase. During the printing process, priority is given by depth, which is performed in the forward order.
(1) print your own name and the first '<'eg:
(2) determine whether there are any attributes. If yes, print them in sequence until the end of Eg:
(3) determine whether to print "/>" or ">" based on whether there are child elements'
(4) Recursion
(5) print finished and close the file
(6) SaveFile return
// (7) save the file
Mydoc-> SaveFile ("star. xml ");
Read an XML file
The previous section briefly analyzed how to use tinyxml to output a DOM tree. Here we will show how to read the XML file and extract the data: the previous document was written, output Dom using tinyxml. The first step of reading the XML file into Dom is to instantiate a tinyxmldocument object.
# Include
# Include "tinyxml. h"
Using namespace std;
Class TiXmlDocument;
Int main (int argc, char ** argv)
{
// (1) create a XML Doc object
TiXmlDocument * myDoc = new TiXmlDocument ("sample. xml ");
This shows how complicated the simplest LoadFile function is when it comes into the system. Open the internal file in the form of "rb" so that TinyXML can be normalized to EOL (reading in binary mode so that tinyxml can normalize the EOL). After the file is successfully opened, the following code can be used to obtain the file size:
Fseek (file, 0, SEEK_END); // LewGun
Length = ftell (file );
Fseek (file, 0, SEEK_SET );
Instantiate a std: string internally (TinyXmlString is used if STL is not used ). Read all the data in the file, read it to a temporary buffer buf, and then normalize the XML file before parsing the XML file, that is: convert "/r/n" or multiple '/R' characters to a single/n character.
If (* p = 0xa)
{
// Newline character. No special rules for this. Append all the characters
// Since the last string, and include the newline.
Data. append (lastpos, (p-lastpos + 1); // append, include the newline
+ + P; // move past the newline
Lastpos = P; // and point to the new buffer (may be 0)
Assert (P <= (BUF + length ));
}
...
Append this to the string you just created. After normalization is completed, the parse function is called for parsing. During the parsing process, the c_str () string is used to process the strings one by one. During the processing, in order to generate an XML document, there will be quite a few spaces to remove it. Based on the extracted characters, different types such as representation are declared, it will be a comment and instantiate different classes. During the instantiation process, the DOM tree is still generated based on the current node, namely, mydoc. When the string comparison is complete, the corresponding DOM tree is also generated.
// (2)
Mydoc-> LoadFile ();
Operations to get and output the root element.
Tixmlelement * rootelement = mydoc-> rootelement ();
Cout <> value () <>
This corresponds to the previous linkendchild operation in theory.
Tixmlelement * firstperson = rootelement-> firstchildelement ();
// Get the first persons's node name & age and attribute ID
Tixmlelement * nameelement = firstperson-> firstchildelement ();
Tixmlelement * ageelement = nameelement-> nextsiblingelement ();
The idea of this Code is that because the attributeset in the element is placed by a two-way linked list, and it has a sentinel node, the node is always placed at the end, and firstattribute () will be called internally
First () {return (Sentinel. Next = & Sentinel )? 0: Sentinel. Next ;}
Last () const {return (Sentinel. Prev = & Sentinel )? 0: Sentinel. Prev ;}
Tixmlattribute * idattriperson = firstperson-> firstattribute ();
In this ageelement/nameelement, the child is a text object, and the value is the previous "Zhou Xing"/20
Cout <> firstchild ()-> value () <>
Cout <> firstchild ()-> value () <>
Cout <> value () <>
}