To use tinyxml, you must include the source code in the project and establish a reference relationship in the header file. The following is a simple example to print the xml file according to the hierarchy.
[Cpp] # include "stdafx. h"
# Include "targetver. h"
# Include "tinystr. h"
# Include "SystemCommon. h"
# Include "tinyxml. h"
Void ParseXML (TiXmlElement * Element );
Int _ tmain (int argc, _ TCHAR * argv [])
{
Char c [2048] = "<! -- This is a comment -->"
"<? Xml version = \ "1.0 \" encoding = \ "UTF-8 \"?> "
"<Class name = \" computer software Class \ "> Computer"
"<Students>"
"<Student name = \" Zhang San \ "studentNo = \" 13031001 \ "sex = \" male \ "age = \" 22 \ ">"
"<Phone> 88208888 </phone>"
"<ET name = \" de \ "ground = \" ground \ "> on the ground </ET>"
"<Et name = \" et name \ "ball = \" ball \ "/>"
"<Address> No. 2 Taibai South Road, Xi'an </address>"
"</Student>"
"<Student name = \" \ "studentNo = \" 13031002 \ "sex = \" male \ "age = \" 20 \ ">"
"<Phone> 88206666 </phone>"
"<Address> Guanghua Road, Xi'an </address>"
"</Student>"
"</Students>"
"</Class> ";
TiXmlDocument * myDocument = new TiXmlDocument ();
// Assume the file name is xml. xml.
MyDocument-> LoadFile ("xml. xml", TIXML_ENCODING_UTF8 );
// MyDocument-> Parse (c, 0, TIXML_ENCODING_UNKNOWN );
TiXmlElement * rootElement = myDocument-> RootElement ();
While (rootElement)
{
ParseXML (rootElement );
RootElement = rootElement-> NextSiblingElement ();
}
Delete myDocument;
Cout <"---------------- END -----------------" <endl;
Return 0;
}
Void PrintTree (int c)
{
If (c <= 0)
Return;
While (c)
{
Cout <"";
-- C;
}
Return;
}
// Call tinyxml to parse xml
Void ParseXML (TiXmlElement * pElement)
{
Static int I = 0;
PrintTree (I );
Cout <pElement-> Value () <"";
Const char * str = NULL;
If (str = pElement-> GetText ())
Cout <"" <str <endl;
Else
Cout <endl;
TiXmlAttribute * attributeOfStudent = pElement-> firstattrident ();
While (attributeOfStudent)
{
PrintTree (I );
Std: cout <attributeOfStudent-> Name () <":" <attributeOfStudent-> Value () <std: endl;
AttributeOfStudent = attributeOfStudent-> Next ();
}
TiXmlElement * ChildElement = pElement-> FirstChildElement ();
While (NULL! = ChildElement)
{
I ++;
ParseXML (ChildElement );
I --;
ChildElement = ChildElement-> NextSiblingElement ();
}
}
# Include "stdafx. h"
# Include "targetver. h"
# Include "tinystr. h"
# Include "SystemCommon. h"
# Include "tinyxml. h"
Void ParseXML (TiXmlElement * Element );
Int _ tmain (int argc, _ TCHAR * argv [])
{
Char c [2048] = "<! -- This is a comment -->"
"<? Xml version = \ "1.0 \" encoding = \ "UTF-8 \"?> "
"<Class name = \" computer software Class \ "> Computer"
"<Students>"
"<Student name = \" Zhang San \ "studentNo = \" 13031001 \ "sex = \" male \ "age = \" 22 \ ">"
"<Phone> 88208888 </phone>"
"<ET name = \" de \ "ground = \" ground \ "> on the ground </ET>"
"<Et name = \" et name \ "ball = \" ball \ "/>"
"<Address> No. 2 Taibai South Road, Xi'an </address>"
"</Student>"
"<Student name = \" \ "studentNo = \" 13031002 \ "sex = \" male \ "age = \" 20 \ ">"
"<Phone> 88206666 </phone>"
"<Address> Guanghua Road, Xi'an </address>"
"</Student>"
"</Students>"
"</Class> ";
TiXmlDocument * myDocument = new TiXmlDocument ();
// Assume the file name is xml. xml.
MyDocument-> LoadFile ("xml. xml", TIXML_ENCODING_UTF8 );
// MyDocument-> Parse (c, 0, TIXML_ENCODING_UNKNOWN );
TiXmlElement * rootElement = myDocument-> RootElement ();
While (rootElement)
{
ParseXML (rootElement );
RootElement = rootElement-> NextSiblingElement ();
}
Delete myDocument;
Cout <"---------------- END -----------------" <endl;
Return 0;
}
Void PrintTree (int c)
{
If (c <= 0)
Return;
While (c)
{
Cout <"";
-- C;
}
Return;
}
// Call tinyxml to parse xml
Void ParseXML (TiXmlElement * pElement)
{
Static int I = 0;
PrintTree (I );
Cout <pElement-> Value () <"";
Const char * str = NULL;
If (str = pElement-> GetText ())
Cout <"" <str <endl;
Else
Cout <endl;
TiXmlAttribute * attributeOfStudent = pElement-> firstattrident ();
While (attributeOfStudent)
{
PrintTree (I );
Std: cout <attributeOfStudent-> Name () <":" <attributeOfStudent-> Value () <std: endl;
AttributeOfStudent = attributeOfStudent-> Next ();
}
TiXmlElement * ChildElement = pElement-> FirstChildElement ();
While (NULL! = ChildElement)
{
I ++;
ParseXML (ChildElement );
I --;
ChildElement = ChildElement-> NextSiblingElement ();
}
}
For example, the file content to be parsed is
<? Xml version = "1.0" encoding = "UTF-8"?>
<! -- This is a comment -->
<Class name = "Class of SoftWare">
<Students>
<Student name = "LiLei" studentNo = "13031001" sex = "Man" age = "22">
<Phone> 88208888 </phone>
<ET name = "de" ground = "ground"> on the ground
<Et name = "et name" ball = "ball"/>
</ET>
<Address> Road1 </address>
</Student>
<Student name = "LiSi" studentNo = "13031002" sex = "Man" age = "20">
<Phone> 88206666 </phone>
<Address> Road2 </address>
</Student>
</Students>
<Teatcher> few teatchers
</Teatcher>
</Class>