Usage and example of tinyxml

Source: Internet
Author: User

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>

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.