CodePudding user response:
http://www.cnblogs.com/ahcc08/p/6082470.htmlCodePudding user response:
General use tinyxmlCodePudding user response:
makeupCodePudding user response:
# include & lt; Iostream>
# include "tinyxml. H"
# include "tinystr. H"
# include & lt; String>
# include & lt; Windows. H>
# include & lt; Atlstr. H>
using namespace std;
Cstrings GetAppPath ()
{//get the application root directory
TCHAR modulePath [MAX_PATH];
GetModuleFileName (NULL, modulePath, MAX_PATH);
Cstrings strModulePath (modulePath);
StrModulePath=strModulePath. Left (strModulePath. ReverseFind (_T (' \ \ ')));
Return strModulePath;
}
Bool CreateXmlFile (string& SzFileName)
{//create an XML file, szFilePath for the path of the file, if create returns true on success, or false
Try
{
//create an XML document object,
TiXmlDocument * myDocument=new TiXmlDocument ();
//create a root element and connections,
TiXmlElement * RootElement=new TiXmlElement (" Persons ");
MyDocument - & gt; LinkEndChild (RootElement);
//create a Person elements and connections,
TiXmlElement * PersonElement=new TiXmlElement (" Person ");
RootElement - & gt; LinkEndChild (PersonElement);
//set the attributes of the Person element,
PersonElement - & gt; The SetAttribute (" ID ", "1");
//create the name element, the age element and connect,
TiXmlElement * NameElement=new TiXmlElement (" name ");
TiXmlElement * AgeElement=new TiXmlElement (" age ");
PersonElement - & gt; LinkEndChild (NameElement);
PersonElement - & gt; LinkEndChild (AgeElement);
//set the name element and the age element content and links,
TiXmlText * NameContent=new TiXmlText (" weeks stars ");
TiXmlText * AgeContent=new TiXmlText (" 22 ");
NameElement - & gt; LinkEndChild (NameContent);
AgeElement - & gt; LinkEndChild (AgeContent);
Cstrings appPath=GetAppPath ();
String seperator="\ ";
String fullPath=appPath. GetBuffer (0) + seperator + szFileName;
MyDocument - & gt; SaveFile (fullPath c_str ());//save to file
}
The catch (string& E)
{
return false;
}
return true;
}
Bool ReadXmlFile (string& SzFileName)
{//read the Xml file and traversal
Try
{
Cstrings appPath=GetAppPath ();
String seperator="\ ";
String fullPath=appPath. GetBuffer (0) + seperator + szFileName;
//create an XML document object,
TiXmlDocument * myDocument=new TiXmlDocument (fullPath c_str ());
MyDocument - & gt; LoadFile ();
//get the root element, namely Persons,
TiXmlElement * RootElement=myDocument - & gt; RootElement ();
//output root element name, that is, output Persons,
Cout & lt; & lt; RootElement - & gt; The Value () & lt;//get the first Person nodes,
TiXmlElement * FirstPerson=RootElement - & gt; FirstChildElement ();
//get the first Person and the age of the name of the node and the ID attribute,
TiXmlElement * NameElement=FirstPerson - & gt; FirstChildElement ();
TiXmlElement * AgeElement=NameElement - & gt; NextSiblingElement ();
TiXmlAttribute * IDAttribute=FirstPerson - & gt; FirstAttribute ();
//output name contents of the first Person, namely weeks stars; The age content, namely; The ID attribute, namely,
Cout & lt;Cout & lt; Cout & lt; }
The catch (string& E)
{
return false;
}
return true;
}
Int main ()
{
XML string fileName="info";
CreateXmlFile (fileName);
ReadXmlFile (fileName);
}
The path of the file is an XML file
CodePudding user response:
Libraries download address here: http://sourceforge.net/projects/tinyxml/