In Visual C ++ tinyxml Quick Start (1), I introduced how to create and print XML files using the tinyxml library, the following describes how to use the tinyxml library to perform a series of operations on XML files, including obtaining XML file declarations, querying specified nodes, deleting specified nodes, modifying specified nodes, and adding nodes. In tinyxml Quick Start (1), we know that a node element in the XML file actually contains two types of values: attribute and text. In my opinion, the attribute can be regarded as a map in STL. An attribute has an attribute value, while a map also has a key and a key value. Therefore, two methods are required to query a specified node, delete a specified node, and add a node. Only one method is required to delete a specified node. In view of the large amount of content, this article introduces how to obtain XML file declarations, query specified nodes, delete specified nodes, and modify and add nodes.
First, get the XML file declaration. The XML file Declaration includes three aspects: version, standalone, and encoding. The source code is as follows:
/*!
*/Brief get the declaration of the XML file.
*
*/Param xmlfile: full path of the XML file.
*/Param strversion version Attribute Value
*/Param strstandalone standalone Attribute Value
*/Param strencoding encoding Attribute Value
*/Return: whether the request is successful. True indicates success, and false indicates failure.
*/
Bool getxmldeclare (STD: String xmlfile,
STD: string & strversion,
STD: string & strstandalone,
STD: string & strencoding)
{
// Define a tixmldocument class pointer
Tixmldocument * pdoc = new tixmldocument ();
If (null = pdoc)
{
Return false;
}
Pdoc-> LoadFile (xmlfile );
Tixmlnode * pxmlfirst = pdoc-> firstchild ();
If (null! = Pxmlfirst)
{
Tixmldeclaration * pxmldec = pxmlfirst-> todeclaration ();
If (null! = Pxmldec)
{
Strversion = pxmldec-> Version ();
Strstandalone = pxmldec-> standalone ();
Strencoding = pxmldec-> encoding ();
}
}
Return true;
}
We found that no matter whether you query a node, delete a node, modify a node, or add a node, you can't do without a function, that is, get the relevant node pointer Based on the node name. Then we will first implement a function to get the node pointer Based on the node name:
/*!
*/Brief obtains the node pointer through the root node and node name.
*
*/Param prootele: Root Node of the XML file.
*/Param strnodename name of the node to be queried
*/Node pointer to be queried by Param Node
*/Return: whether to find the value. True indicates that the corresponding node pointer is found, and false indicates that the corresponding node pointer is not found.
*/
Bool getnodepointerbyname (tixmlelement * prootele, STD: string & strnodename, tixmlelement * & node)
{
// Exit if it is equal to the root node name
If (strnodename = prootele-> value ())
{
Node = prootele;
Return true;
}
Tixmlelement * Pele = prootele;
For (Pele = prootele-> firstchildelement (); Pele = Pele-> nextsiblingelement ())
{
// Recursively process subnodes and obtain node pointers
If (getnodepointerbyname (Pele, strnodename, node ))
Return true;
}
Return false;
}
With this function, we can easily query the corresponding text or attribute values of a node.
/*!
*/Brief queries nodes.
*
*/Param xmlfile: full path of the XML file.
*/Param strnodename name of the node to be queried
*/Param strtext: the node text to be queried
*/Return: whether the request is successful. True indicates success, and false indicates failure.
*/
Bool querynode_text (STD: String xmlfile, STD: String strnodename, STD: string & strtext)
{
// Define a tixmldocument class pointer
Tixmldocument * pdoc = new tixmldocument ();
If (null = pdoc)
{
Return false;
}
Pdoc-> LoadFile (xmlfile );
Tixmlelement * prootele = pdoc-> rootelement ();
If (null = prootele)
{
Return false;
}
Tixmlelement * pnode = NULL;
Getnodepointerbyname (prootele, strnodename, pnode );
If (null! = Pnode)
{
Strtext = pnode-> gettext ();
Return true;
}
Else
{
Return false;
}
}
/*!
*/Brief queries nodes.
*
*/Param xmlfile: full path of the XML file.
*/Param strnodename name of the node to be queried
*/Param attmap: the attribute value to be queried. this is a map. The first one is the attribute name, and the last one is the attribute value.
*/Return: whether the request is successful. True indicates success, and false indicates failure.
*/
Bool querynode_attribute (STD: String xmlfile, STD: String strnodename, STD: Map <STD: String, STD: String> & attmap)
{
// Define a tixmldocument class pointer
Typedef STD: pair <STD: String, STD: String> string_pair;
Tixmldocument * pdoc = new tixmldocument ();
If (null = pdoc)
{
Return false;
}
Pdoc-> LoadFile (xmlfile );
Tixmlelement * prootele = pdoc-> rootelement ();
If (null = prootele)
{
Return false;
}
Tixmlelement * pnode = NULL;
Getnodepointerbyname (prootele, strnodename, pnode );
If (null! = Pnode)
{
Tixmlattribute * pattr = NULL;
For (pattr = pnode-> firstattribute (); pattr = pattr-> next ())
{
STD: String strattname = pattr-> name ();
STD: String strattvalue = pattr-> value ();
Attmap. insert (string_pair (strattname, strattvalue ));
}
Return true;
}
Else
{
Return false;
}
Return true;
}
The following is a function used to delete a specified node. The deletion of the root node is considered:
/*!
*/Brief deletes the value of a specified node.
*
*/Param xmlfile: full path of the XML file.
*/Param strnodename specifies the node name.
*/Return: whether the request is successful. True indicates success, and false indicates failure.
*/
Bool delnode (STD: String xmlfile, STD: String strnodename)
{
// Define a tixmldocument class pointer
Tixmldocument * pdoc = new tixmldocument ();
If (null = pdoc)
{
Return false;
}
Pdoc-> LoadFile (xmlfile );
Tixmlelement * prootele = pdoc-> rootelement ();
If (null = prootele)
{
Return false;
}
Tixmlelement * pnode = NULL;
Getnodepointerbyname (prootele, strnodename, pnode );
// If it is a root node
If (prootele = pnode)
{
If (pdoc-> removechild (prootele ))
{
Pdoc-> SaveFile (xmlfile );
Return true;
}
Else
Return false;
}
// If it is another node
If (null! = Pnode)
{
Tixmlnode * pparnode = pnode-> parent ();
If (null = pparnode)
{
Return false;
}
Tixmlelement * pparentele = pparnode-> toelement ();
If (null! = Pparentele)
{
If (pparentele-> removechild (pnode ))
Pdoc-> SaveFile (xmlfile );
Else
Return false;
}
}
Else
{
Return false;
}
Return false;
}