XML files are essentially small databases. In other words, what operations do you have on the database? What operations should you perform on XML files. Generally, database operations include creating a database, querying a database, modifying a database, and deleting a database. The corresponding XML file is used to create an XML file, query the value of a specified node in the XML file, modify the value of a node in the XML file, and delete the value of a node in the XML file.
First, let's take a look at the XML file formats. Below I will list some common XML files:
Example1.xml:
<? XML version = "1.0"?>
<Hello> world </Hello>
Example2.xml:
<? XML version = "1.0"?>
<Poetry>
<Verse>
Alas
Great World
Alas (again)
</Verse>
</Poetry>
Example3.xml:
<? XML version = "1.0"?>
<Shapes>
<Circle name = "int-based" x = "20" Y = "30" r = "50"/>
<Point name = "float-based" x = "3.5" Y = "52.1"/>
</Shapes>
Example4.xml:
<? XML version = "1.0"?>
<MyApp>
<Messages>
<Welcome> welcome to MyApp </welcome>
<Farewell> Thank you for using MyApp </farewell>
</Messages>
<Windows>
<Window name = "mainframe" x = "5" Y = "15" W = "400" H = "250"/>
</Windows>
<Connection IP = "192.168.0.1" timeout = "123.456000"/>
</MyApp>
The above example is taken from tinyxml tutorial Chinese guide. There are four examples above. How many forms of XML files do you see? I see that the attribute value is essentially in two forms: the attribute value is in the angle brackets, for example, if <window name = "mainframe" x = "5" Y = "15" W = "400" H = "250"/> and the text is out of angle brackets, for example, <welcome> welcome to MyApp </welcome>.
In view of the complexity of example4.xml, I will introduce the use of tinyxml as an example.
Tinyxml uses two compilation options: Use the char * type of standard C or use STD: String in STL. Use the pre-processor tixml_use_stl for control, that is, tixml_use_stl is added to use STD:: string. In view of the wide use of STL and its powerful functions, I will describe tinyxml Using STD: string.
Use vs 2010 to open the tinyxmlstl. DSP project file, compile it into a static library, debug version: tinyxmld_stl.lib, and then test the tinyxml library. My test plan is as follows: first, use the tinyxml library to create example4.xml, read it, and then query the attributes or text of the specified node, modify example4.xml (modify some node values, delete one of the nodes, and add one), and then read it to determine whether the modification is successful. Specifically, create a new console project in vs 2010: Test. Be sure to use the multi-Byte Character Set for compilation and add it at the same time. First, the code for creating an XML file:
/*!
*/Brief: Create an XML file.
*
*/Param xmlfile: full path of the XML file.
*/Return: whether the request is successful. True indicates success, and false indicates failure.
*/
Bool createxml (STD: String xmlfile)
{
// Define a tixmldocument class pointer
Tixmldocument * pdoc = new tixmldocument;
If (null = pdoc)
{
Return false;
}
Tixmldeclaration * pdeclaration = new tixmldeclaration (_ T ("1.0"), _ T (""), _ T (""));
If (null = pdeclaration)
{
Return false;
}
Pdoc-> linkendchild (pdeclaration );
// Generate a root node: MyApp
Tixmlelement * prootele = new tixmlelement (_ T ("MyApp "));
If (null = prootele)
{
Return false;
}
Pdoc-> linkendchild (prootele );
// Generate a subnode: messages
Tixmlelement * PMSG = new tixmlelement (_ T ("messages "));
If (null = PMSG)
{
Return false;
}
Prootele-> linkendchild (PMSG );
// Generate the subnode: Welcome
Tixmlelement * pwelcome = new tixmlelement (_ T ("welcome "));
If (null = pwelcome)
{
Return false;
}
PMSG-> linkendchild (pwelcome );
// Set the value of the welcome Node
STD: String strvalue = _ T ("Welcome to MyApp ");
Tixmltext * pwelcomevalue = new tixmltext (strvalue );
Pwelcome-> linkendchild (pwelcomevalue );
// Generate a subnode: farewell
Tixmlelement * pfarewell = new tixmlelement (_ T ("farewell "));
If (null = pfarewell)
{
Return false;
}
PMSG-> linkendchild (pfarewell );
// Set the value of the farewell Node
Strvalue = _ T ("Thank you for using MyApp ");
Tixmltext * pfarewellvalue = new tixmltext (strvalue );
Pfarewell-> linkendchild (pfarewellvalue );
// Generate a subnode: Windows
Tixmlelement * pwindows = new tixmlelement (_ T ("Windows "));
If (null = pwindows)
{
Return false;
}
Prootele-> linkendchild (pwindows );
// Generate the subnode: Window
Tixmlelement * pwindow = new tixmlelement (_ T ("window "));
If (null = pwindow)
{
Return false;
}
Pwindows-> linkendchild (pwindow );
// Set the value of the node window
Pwindow-> setattribute (_ T ("name"), _ T ("mainframe "));
Pwindow-> setattribute (_ T ("X"), _ T ("5 "));
Pwindow-> setattribute (_ T ("Y"), _ T ("15 "));
Pwindow-> setattribute (_ T ("W"), _ T ("400 "));
Pwindow-> setattribute (_ T ("H"), _ T ("250 "));
// Generate the subnode: Window
Tixmlelement * pconnection = new tixmlelement (_ T ("connection "));
If (null = pconnection)
{
Return false;
}
Prootele-> linkendchild (pconnection );
// Set the connection value of the node.
Pconnection-> setattribute (_ T ("ip"), _ T ("192.168.0.1 "));
Pconnection-> setattribute (_ T ("timeout"), _ T ("123.456000 "));
Pdoc-> SaveFile (xmlfile );
Return true;
}
I wonder if you noticed the rule above? First, the parent node connects to the byte point and uses the linkendchild function. The usage is: pparentnode-> linkendchild (pchild ); second, set a structure like this <window name = "mainframe" x = "5" Y = "15" W = "400" H = "250"/> using the setattribute function, this function has two parameters. The first parameter represents the key, and the last parameter represents the key value. Set <farewell> Thank you for using
MyApp </farewell> adopts the tixmltext class and uses the linkendchild function for link.
The above is the code for creating an XML file. The following describes the code for reading an XML file. The code for printing the entire XML file is simple. The Code is as follows:
/*!
*/Brief prints the XML file.
*
*/Param xmlfile: full path of the XML file.
*/Return: whether the request is successful. True indicates success, and false indicates failure.
*/
Bool paintxml (STD: String xmlfile)
{
// Define a tixmldocument class pointer
Tixmldocument * pdoc = new tixmldocument ();
If (null = pdoc)
{
Return false;
}
Pdoc-> LoadFile (xmlfile );
Pdoc-> Print ();
Return true;
}
Next, we will introduce how to use the tinyxml library to query specified nodes, delete specified nodes, modify specified nodes, and add nodes.
Visual c ++ tinyxml Quick Start (2)
Visual c ++ tinyxml Quick Start (3)