In Visual C ++ tinyxml Quick Start (2), this article introduces how to use the tinyxml library to obtain XML file declarations, query specified nodes, and delete specified nodes. This article describes how to modify a specified node and add a node.
Modifying a node is actually a bit similar to querying the value of a specified node. It is also divided into two functions, one for modifying the text. The other is responsible for modifying attributes.
/*!
*/Brief modify the text of the specified node.
*
*/Param xmlfile: full path of the XML file.
*/Param strnodename specifies the node name.
*/Param strtext reset Text Value
*/Return: whether the request is successful. True indicates success, and false indicates failure.
*/
Bool modifynode_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)
{
Pnode-> clear (); // first clear all text
// Insert the text and save the file
Tixmltext * pvalue = new tixmltext (strtext );
Pnode-> linkendchild (pvalue );
Pdoc-> SaveFile (xmlfile );
Return true;
}
Else
Return false;
}
/*!
*/Brief modify the attribute value of a specified node.
*
*/Param xmlfile: full path of the XML file.
*/Param strnodename specifies the node name.
*/Param attmap: reset the attribute value. 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 modifynode_attribute (STD: String xmlfile, STD: String strnodename,
STD: Map <STD: String, STD: String> & attmap)
{
Typedef STD: pair <STD: String, STD: String> string_pair;
// 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)
{
Tixmlattribute * pattr = NULL;
STD: String strattname = _ T ("");
STD: String strattvalue = _ T ("");
For (pattr = pnode-> firstattribute (); pattr = pattr-> next ())
{
Strattname = pattr-> name ();
STD: Map <STD: String, STD: String >:: iterator ITER;
For (iter = attmap. Begin (); iter! = Attmap. End (); ITER ++)
{
If (strattname = ITER-> first)
{
Pattr-> setvalue (ITER-> second );
}
}
}
Pdoc-> SaveFile (xmlfile );
Return true;
}
Else
{
Return false;
}
}
For the modifynode_attribute function, the following section describes how to use it, for example, the following XML file:
<? XML version = "1.0" encoding = "UTF-8" standalone = "yes"?>
<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>
If we want to modify the connection IP address of the node to 192.168.0.100 and timeout to 1000, we can use the following method:
STD: String xmlfile = _ T ("E: // testtinyxml // example4.xml ");
STD: String strnodename = _ T ("connection ");
Typedef STD: pair <STD: String, STD: String> string_pair;
STD: Map <STD: String, STD: String> attmap;
Attmap. insert (string_pair (_ T ("ip"), _ T ("192.168.0.100 ")));
Attmap. insert (string_pair (_t ("timeout"), _ T ("1000 ")));
Modifynode_attribute (xmlfile, strnodename, attmap );
The following are two functions for adding nodes:
/*!
*/Brief adds the text of the specified node.
*
*/Param xmlfile: full path of the XML file.
*/Param strparnodename: parent node of the node to be added.
*/Param strnodename specifies the node name.
*/Param strtext text to be added
*/Return: whether the request is successful. True indicates success, and false indicates failure.
*/
Bool addnode_text (STD: String xmlfile, STD: String strparnodename, 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, strparnodename, pnode );
If (null! = Pnode)
{
// Generate a subnode: pnewnode
Tixmlelement * pnewnode = new tixmlelement (strnodename );
If (null = pnewnode)
{
Return false;
}
// Set the node text and insert the node
Tixmltext * pnewvalue = new tixmltext (strtext );
Pnewnode-> linkendchild (pnewvalue );
Pnode-> insertendchild (* pnewnode );
Pdoc-> SaveFile (xmlfile );
Return true;
}
Else
Return false;
}
/*!
*/Brief add nodes.
*
*/Param xmlfile: full path of the XML file.
*/Param strparnodename: parent node of the node to be added.
*/Param strnodename specifies the node name.
*/Param attmap specifies the attribute value of the node to be added. 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 addnode_attribute (STD: String xmlfile, STD: String strparnodename, STD: String strnodename, STD: Map <STD: String, STD: String> & attmap)
{
// 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, strparnodename, pnode );
If (null! = Pnode)
{
// Generate a subnode: pnewnode
Tixmlelement * pnewnode = new tixmlelement (strnodename );
If (null = pnewnode)
{
Return false;
}
// Set the node property value and insert the node
STD: Map <STD: String, STD: String >:: iterator ITER;
For (iter = attmap. Begin (); iter! = Attmap. End (); ITER ++)
{
Pnewnode-> setattribute (ITER-> first, ITER-> second );
}
Pnode-> insertendchild (* pnewnode );
Pdoc-> SaveFile (xmlfile );
Return true;
}
Else
Return false;
}