TinyXml使用指南
- 格式:doc
- 大小:49.50 KB
- 文档页数:6
TinyXml使用指南(1) 作者:tamsyn 来源: 时间:2006-8-16 【字体:大中小】〖双击滚屏〗在这里我们并不是要讲解如何使用XML,或在网页中使用它。
而是要在C/C++中使用它。
详细一点就是在C/C++嵌套使用XML文件。
要在C/C++中使用XML文件,你就必须需要一个能分析XML文件的函数库。
在这方面有很多,比如libxml2,tinxml,expat等等很多。
而我使用的是tinyxml,为什么要使用它呢?因为它很小巧,只有两个头文件和四个CPP文件。
正如作者在tinyxml官方文件中所说的,如果你不是想在浏览器中使用XML,那么tinyxml非常适合你。
我们下面来看一下tinyxml是如何在C/C++中建立XML文件的。
char floader[200],buffer[200];TiXmlDocument* m_xmlDoc = new TiXmlDocument();TiXmlElement xElement("player");sprintf(buffer,"%d", 1);xElement.SetAttribute("admin", buffer);TiXmlElement xPos("pos");sprintf(buffer,"%d",2);xPos.SetAttribute("x", buffer);sprintf(buffer,"%d",3);xPos.SetAttribute("y", buffer);sprintf(buffer,"%d",4);xPos.SetAttribute("zone", buffer);xElement.InsertEndChild(xPos);m_xmlDoc->InsertEndChild(xElement);sprintf(floader,"%s.xml", "antking");m_xmlDoc->SaveFile(floader);delete m_xmlDoc;这段简单的代码的作用就是将用户数据保存在XML文件中。
不同的是我对其做了一些简化。
这段代码的作用就是先建立一个XML文件句柄。
TiXmlDocument* m_xmlDoc = new TiXmlDocument();然后建立一个成员。
TiXmlElement xElement("player");sprintf(buffer,"%d", 1);xElement.SetAttribute("admin", buffer);然后再建立一个成员。
TiXmlElement xPos("pos");sprintf(buffer,"%d",2);xPos.SetAttribute("x", buffer);sprintf(buffer,"%d",3);xPos.SetAttribute("y", buffer);sprintf(buffer,"%d",4);xPos.SetAttribute("zone", buffer);这个成员包含3个属性。
接下来是将这个成员连接成上一个成员的子结点。
xElement.InsertEndChild(xPos);然后再把上一个结点连接成为XML文件的子结点。
m_xmlDoc->InsertEndChild(xElement);最后保存这个文件。
sprintf(floader,"%s.xml", "antking");m_xmlDoc->SaveFile(floader);释放句柄。
delete m_xmlDoc;这就是创建XML文件的过程。
这段代码将产生一个文件,文件内容如下:<pos x="2" y="3" zone="4" ></pos x="2" y="3" zone="4" >它产生的结构可以用一棵树来表示,见下图:接下来,我们看一下如何从上面这个XML文件中读出数据。
char floader[200],buffer[200];TiXmlDocument* m_xmlDoc;int admin1,x1,y1,z1;sprintf(floader,"%s.xml", "antking");m_xmlDoc = new TiXmlDocument(floader);if (m_xmlDoc->LoadFile()){TiXmlElement *xPlayer = 0;xPlayer = m_xmlDoc->FirstChildElement("player");if (xPlayer){if (xPlayer->Attribute("admin"))admin1= (bool)atoi(xPlayer->Attribute("admin"));TiXmlElement *xZone = 0;xZone = xPlayer->FirstChildElement("pos");x1 = (int)atoi(xZone->Attribute("x"));y1 = (int)atoi(xZone->Attribute("y"));z1 = (int)atoi(xZone->Attribute("zone"));}}delete m_xmlDoc;printf("%d,%d,%d,%d",admin1,x1,y1,z1);这段代码的意思就是,先建立一个文件句柄,如果句柄建立成功就打开一个XML文件。
XML 文件打开后,先得到它的第一个子结点,如果子结点存在,得到这个结点的属性。
然后又打开这个结点的第一个子结点,读出其中的数据。
最后关闭文件句柄。
#include "iostream"#include "fstream"#include "tinyxml.h"using namespace std;int main(){string filename = "first.xml";TiXmlDocument* doc = new TiXmlDocument(filename.c_str());////////////////////////////////////////////////////////////////////////// // 在这里复制文件////////////////////////////////////////////////////////////////////////// std::ifstream ifs(filename.c_str());char buffer[1024];char c, *p = buffer;while(ifs.get(c)){*p++=c;}*p = 0;ifs.close();//////////////////////////////////////////////////////////////////////////if(!doc->Parse(buffer)){cout << doc->ErrorDesc() << endl;}const TiXmlElement* root = doc->RootElement();for( const TiXmlNode* child = root->FirstChild();child;child=child->NextSibling()){OutputDebugStringA(child->Value());/*生成一个StaticBox*/if((child->Type() == TiXmlNode::ELEMENT) &&(!strcmp(child->Value(),"staticbox"))){const TiXmlElement *box = (const TiXmlElement*)child;double px, py, pz;double dx, dy, dz;std::string mesh;mesh = box->Attribute("mesh");for(const TiXmlNode *sub_tag = box->FirstChild(); sub_tag; sub_tag = sub_tag->NextSibling() ){if(sub_tag->Type() == TiXmlNode::ELEMENT){const TiXmlElement *sub_element = (const TiXmlElement*)sub_tag;if(!strcmp(sub_tag->Value(),"position")){px = (sub_element->Attribute("x",&px))?px:0.0;py = (sub_element->Attribute("y",&py))?py:0.0;pz = (sub_element->Attribute("z",&pz))?pz:0.0;}else if(!strcmp(sub_tag->Value(),"dimension")){dx = (sub_element->Attribute("x",&dx))?dx:1.0;dy = (sub_element->Attribute("y",&dy))?dy:1.0;dz = (sub_element->Attribute("z",&dz))?dz:1.0;}}}cout << "\n";cout << "\tPosition = (" << px << ", " << py << ", " << pz << ")\n"; cout << "\tDimension = (" << dx << ", " << dy << ", " << dz << ")\n\n"; }}delete doc;getchar();return 0;}然后在项目的文件夹中加入一个xml文件,如下:<?xml version="1.0" encoding="utf-8" ?><Scene><staticbox mesh="crate.mesh"><position x="-8" y="2" z="4" /><dimension x="2" y="4" z="2" /></staticbox><staticbox mesh="crate.mesh"><position x="3" y="2" z="4" /><dimension x="2" y="4" z="2" /></staticbox> </Scene>。