使用tinyxml,需要在工程中包含其源碼,並在頭文件建立引用關系。下面是一個簡單的例子,按照層次關系打印出xml文件。
[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=\"計算機軟件班\">計算機"
"<Students>"
"<student name=\"張三\" studentNo=\"13031001\" sex=\"男\" age=\"22\">"
"<phone>88208888</phone>"
"<ET name=\"de\" ground = \"ground\">on the ground</ET>"
"<et name = \"et name\" ball = \"ball\"/>"
"<address>西安市太白南路二號</address>"
"</student>"
"<student name=\"李四\" studentNo=\"13031002\" sex=\"男\" age=\"20\">"
"<phone>88206666</phone>"
"<address>西安市光華路</address>"
"</student>"
"</Students>"
"</Class>";
TiXmlDocument *myDocument = new TiXmlDocument();
//假設文件名是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;
}
//調用 tinyxml 解析 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->FirstAttribute();
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=\"計算機軟件班\">計算機"
"<Students>"
"<student name=\"張三\" studentNo=\"13031001\" sex=\"男\" age=\"22\">"
"<phone>88208888</phone>"
"<ET name=\"de\" ground = \"ground\">on the ground</ET>"
"<et name = \"et name\" ball = \"ball\"/>"
"<address>西安市太白南路二號</address>"
"</student>"
"<student name=\"李四\" studentNo=\"13031002\" sex=\"男\" age=\"20\">"
"<phone>88206666</phone>"
"<address>西安市光華路</address>"
"</student>"
"</Students>"
"</Class>";
TiXmlDocument *myDocument = new TiXmlDocument();
//假設文件名是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;
}
//調用 tinyxml 解析 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->FirstAttribute();
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();
}
}
例如要解析的文件內容是
<?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>