tinyxml的用法和实例 (一)

2014-11-24 01:41:29 · 作者: · 浏览: 4

使用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] = ""
"< xml version=\"1.0\" encoding=\"utf-8\" >"
"计算机"
""
""
"88208888"
"on the ground"
""
"

西安市太白南路二号
"
""
""
"88206666"
"
西安市光华路
"
"
"
""
"";
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-----------------"< 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<Value()<<" ";
const char * str = NULL;
if(str = pElement->GetText())
cout<<" "< else
cout<
TiXmlAttribute* attributeOfStudent = pElement->FirstAttribute();
while(attributeOfStudent)
{
PrintTree(i);
std::cout << attributeOfStudent->Name() << " : " << attributeOfStudent->Value()< 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] = ""
"< xml version=\"1.0\" encoding=\"utf-8\" >"
"计算机"
""
""
"88208888"
"on the ground"
""
"

西安市太白南路二号
"
""
""
"88206666"
"
西安市光华路
"
"
"
""
"";
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-----------------"< 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<Value()<<" ";
const char * str = NULL;
if(str = pElement->GetText())
cout<<" "< else
cout<

TiXmlAttribute* attributeOfStud