您的位置:首页 > 其它

Tinyxml的简单用法

2013-04-27 13:56 176 查看
生成或解析的XML如下(代码的头文件未包含,只是测试,所以未检查空指针):

View Code

#include "ParseXml.h"
#include "tinyxml.h"
#include <iostream>

using std::cout;

int ParseXml::ParseCfgXml()
{
TiXmlDocument* myXmlDocument = new TiXmlDocument("test.xml");
myXmlDocument->LoadFile();
myXmlDocument->Print();

//获取根节点,你也可以用FirstChildElement()来获取
TiXmlElement* rootElement = myXmlDocument->RootElement();
TiXmlElement* StuElement = rootElement->FirstChildElement();
//获取节点属性
TiXmlAttribute* StuAttr = StuElement->FirstAttribute();
const char* Name = StuAttr->Value();
StuAttr = StuAttr->Next();
const char* Age = StuAttr->Value();
StuAttr = StuAttr->Next();
const char* Male = StuAttr->Value();

cout<< "The Student is " << Name << ", Age is " << Age << ", Sex is " << Male << std::endl;

StuElement = StuElement->FirstChildElement();
const char* Addr = StuElement->GetText();

//获取注释
TiXmlNode* myNode = StuElement->LastChild();
TiXmlComment* AddrComment = myNode->ToComment();
const char* Comment = AddrComment->Value();

cout << Comment << " is " << Addr << std::endl;

StuElement = StuElement->NextSiblingElement();
const char* Tel = StuElement->GetText();

cout << "The Tel is " << Tel << std::endl;

if (NULL != myXmlDocument)
{
delete myXmlDocument;
myXmlDocument = NULL;
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: