标签:des style blog http color 使用 os io
#include "stdafx.h" #include <iostream> #include <fstream> #include "tinyxml.h" using namespace std; int _tmain(int argc, _TCHAR* argv[]) { //原先代码是直接加载XML文件,我作了一下修改,把内容读到字符串后再解析,实际使用时就去掉读取XML文件这一步 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(); ////////////////////////////////////////////////////////////////////////// //这里开始从字符串中解析XML //创建TiXmlDocument对象 TiXmlDocument* doc = new TiXmlDocument(); //解析 if(!doc->Parse(buffer)) { cout << doc->ErrorDesc() << endl; } //获取根节点 const TiXmlElement* root = doc->RootElement(); //循环获取该根节点下面的节点 for( const TiXmlNode* child = root->FirstChild(); child; child=child->NextSibling()) { //判断为元素类型并且是staticbox元素,Value()获取该标签的名称 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; } else if(!strcmp(sub_tag->Value(),"test")) { //使用GetText()方法来获取该标签的值,如这里获取的是test的值1和2 string temp = sub_element->GetText(); } } } cout << "<StaticBox>\n"; cout << "\tPosition = (" << px << ", " << py << ", " << pz << ")\n"; cout << "\tDimension = (" << dx << ", " << dy << ", " << dz << ")\n\n"; } } delete doc; getchar(); return 0; }
7.然后在项目的文件夹中加入一个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" /> <test>1</test> </staticbox> <staticbox mesh="crate.mesh"> <position x="3" y="2" z="4" /> <dimension x="2" y="4" z="2" /> <test>2</test> </staticbox> </Scene>
#define TIXML_USE_STL //标志使用STL的内容 #include "stdafx.h" #include <iostream> #include <sstream> #include <fstream> #include "tinyxml.h" using namespace std; int WriteXML() ; int ReadXML() ; //读取内存里的一段XML int ReadXML2() ; int _tmain(int argc, _TCHAR* argv[]) { cout << "开始测试!" <<endl; WriteXML() ; ReadXML() ; ReadXML2() ; cout << "结束测试!" << endl ; return 0; } int WriteXML() { TiXmlDocument xmlDoc( "test.xml" ); // 建立一个XML文件 TiXmlDeclaration Declaration( "1.0","gb2312", "yes" ); // 声明XML的属性 xmlDoc.InsertEndChild( Declaration ); // 写入基本的XML头结构 TiXmlNode * pNode = NULL; TiXmlElement* pRootElm = NULL; TiXmlElement* pChildeElm = NULL; TiXmlElement* pItemElm = NULL; TiXmlText* pText = NULL; // 一个指向Text的指针 pText = new TiXmlText("good") ; pRootElm = new TiXmlElement( "todo" ); pNode = xmlDoc.InsertEndChild(*pRootElm) ; pRootElm = pNode->ToElement() ; pChildeElm = new TiXmlElement("child1") ; pChildeElm->SetAttribute("num", 9) ; pNode = pRootElm->InsertEndChild(*pChildeElm) ; TiXmlElement* pChildeElm1 = NULL; pChildeElm1 = pNode->ToElement() ; pChildeElm1->InsertEndChild(*pText) ; delete pChildeElm ; pChildeElm = NULL ; pChildeElm = new TiXmlElement("child2") ; pChildeElm->SetAttribute("num", 10) ; pNode = pRootElm->InsertBeforeChild (pChildeElm1, *pChildeElm) ; TiXmlElement* pChildeElm2 = NULL; pChildeElm2 = pNode->ToElement() ; pChildeElm2->InsertEndChild(*pText) ; xmlDoc.Print() ; xmlDoc.SaveFile(); // 把XML文件写入硬盘 return 0 ; } int ReadXML() { cout<<endl ; cout<<"开始read XML"<<endl ; TiXmlDocument xmlDoc( "test.xml" ); xmlDoc.LoadFile() ; TiXmlElement* xmlRootElement = 0 ; TiXmlElement* xmlSubElement = 0 ; TiXmlNode * pNode = NULL; pNode = xmlDoc.FirstChild("todo") ; xmlRootElement = pNode->ToElement() ; if(xmlRootElement) { pNode = xmlRootElement->FirstChild("child1") ; xmlSubElement = pNode->ToElement() ; cout<<xmlSubElement->Value()<<endl ; cout<<xmlSubElement->Attribute("num")<<endl ; cout<<xmlSubElement->GetText()<<endl ; } else { cout<<"找不到根元素"<<endl ; } cout<<"结束read XML"<<endl ; return 0 ; } int ReadXML2() { cout<<endl ; cout<<"开始read XML"<<endl; const char* demoEnd ="<?xml version=\"1.0\" encoding=\"gb2312\" standalone=\"yes\" ?>" "<todo>" "<child2 num=\"10\">good</child2>" "<child1 num=\"9\">good</child1>" "</todo>" ; TiXmlDocument xmlDoc; xmlDoc.Parse( demoEnd ) ; TiXmlElement* xmlRootElement = 0 ; TiXmlElement* xmlSubElement = 0 ; TiXmlNode * pNode = NULL; pNode = xmlDoc.FirstChild("todo") ; xmlRootElement = pNode->ToElement() ; if(xmlRootElement) { pNode = xmlRootElement->FirstChild("child1") ; xmlSubElement = pNode->ToElement() ; cout<<xmlSubElement->Value()<<endl ; cout<<xmlSubElement->Attribute("num")<<endl ; cout<<xmlSubElement->GetText()<<endl ; } else { cout<<"找不到根元素"<<endl ; } cout<<"结束read XML"<<endl ; return 0 ; }
原文地址:http://blog.chinaunix.net/uid-22240661-id-1781609.html
VC环境使用XML解析器(TinyXML)编程,布布扣,bubuko.com
标签:des style blog http color 使用 os io
原文地址:http://www.cnblogs.com/guobbs/p/3901325.html