/*
TinyXML 官網
http://sourceforge.net/projects/tinyxml/
在TinyXML中,根據XML的各種元素來定義了一些類:
TiXmlBase:整個TinyXML模型的基類。
TiXmlAttribute:對應于XML中的元素的屬性。
TiXmlNode:對應于DOM結構中的節點。
TiXmlComment:對應于XML中的注釋
TiXmlDeclaration:對應于XML中的申明部分,如:<?xml version="1.0" encoding="UTF-8"?>
TiXmlDocument:對應于XML的整個文檔。
TiXmlElement:對應于XML的元素。
TiXmlText:對應于XML的文字部分
TiXmlUnknown:對應于XML的未知部分。
TiXmlHandler:定義了針對XML的一些操作。
關鍵宏:TIXML_USE_STL
我是直接在tinyxml.h 中直接 #define TIXML_USE_STL
//xml文檔
<?xml version="1.0" standalone="no" ?>
<!-- Our to do list data -->
<ToDo>
<!-- Do I need a secure PDA? -->
<Item priority="1" distance="close">菜單1
<bold>Toy store!</bold>
</Item>
<Item priority="2" distance="none">菜單2</Item>
<Item priority="2" distance="far & back">Look for Evil Dinosaurs!</Item>
</ToDo>
*/
#include <stdio.h>
using namespace std;
#include "tinyxml.h"
int main()
{
int nVale ;
int nRet ;
string strValue;
TiXmlDocument doc( "demotest.xml" );
bool loadOkay = doc.LoadFile();
if ( !loadOkay )
{
printf( "Could not load test file 'demotest.xml'. Error='%s'. Exiting.\n", doc.ErrorDesc() );
exit( 1 );
}
TiXmlNode* node = 0;
TiXmlElement* rootElement = 0;
TiXmlElement* itemElement = 0;
TiXmlElement* childElement = 0;
rootElement = doc.RootElement();
assert( rootElement );
cout << rootElement->Value() << endl;
itemElement = rootElement->FirstChildElement();
cout << itemElement->Value() << endl;
cout << itemElement->FirstAttribute()->Value() << endl;
nRet = itemElement->QueryIntAttribute("priority",&nVale);
if (nRet==0) {
cout << nVale << endl;
}
nRet = itemElement->QueryStringAttribute("distance",&strValue);
if (nRet==0) {
cout << strValue << endl;
}
strValue = itemElement->GetText();
cout << strValue << endl;
childElement = itemElement->FirstChildElement();
if (childElement!=NULL)
{
strValue = childElement->Value();
cout << strValue << endl;
strValue = childElement->GetText();
cout << strValue << endl;
}
itemElement = itemElement->NextSiblingElement();
assert(itemElement);
strValue = itemElement->GetText();
cout << strValue << endl;
return 0;
}