加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

tinyxml读写xml示例代码

发布时间:2020-12-16 09:27:59 所属栏目:百科 来源:网络整理
导读:tinyxml读写xml示例代码 http://www.seanyxie.com/tinyxml%E8%AF%BB%E5%86%99xml%E7%A4%BA%E4%BE%8B%E4%BB%A3%E7%A0%81/ TinyXML是一个开源的解析XML的解析库,能够用于C++,能够在Windows或Linux中编译。这个解析库的模型通过解析XML文件,然后在内存中生成

tinyxml读写xml示例代码


http://www.seanyxie.com/tinyxml%E8%AF%BB%E5%86%99xml%E7%A4%BA%E4%BE%8B%E4%BB%A3%E7%A0%81/


TinyXML是一个开源的解析XML的解析库,能够用于C++,能够在Windows或Linux中编译。这个解析库的模型通过解析XML文件,然后在内存中生成DOM模型,从而让我们很方便的遍历这棵XML树。

DOM模型即文档对象模型,是将整个文档分成多个元素(如书、章、节、段等),并利用树型结构表示这些元素之间的顺序关系以及嵌套包含关系。

如下是一个XML片段:

<Persons>

<PersonID=”1″>

<name>周星星</name>

<age>20</age>

</Person>

<PersonID=”2″>

<name>白晶晶</name>

<age>18</age>

</Persons>

在TinyXML中,根据XML的各种元素来定义了一些类:

TiXmlBase:整个TinyXML模型的基类。

TiXmlAttribute:对应于XML中的元素的属性。

TiXmlNode:对应于DOM结构中的节点。

TiXmlComment:对应于XML中的注释

TiXmlDeclaration:对应于XML中的申明部分,即<?versiong=”1.0″?>。

TiXmlDocument:对应于XML的整个文档。

TiXmlElement:对应于XML的元素。

TiXmlText:对应于XML的文字部分

TiXmlUnknown:对应于XML的未知部分。

TiXmlHandler:定义了针对XML的一些操作。

TinyXML是个解析库,主要由DOM模型类(TiXmlBase、TiXmlNode、TiXmlAttribute、TiXmlComment、TiXmlDeclaration、TiXmlElement、TiXmlText、TiXmlUnknown)和操作类(TiXmlHandler)构成。

tinyxml组件读写XML非常方便,相比boost更加轻便。

下面是xml读写的模版文件


<?xmlversion="1.0"encoding="utf-8"standalone="yes"?><savemaplist>
<mapmapid="1"name="Dota版本v8.w3x"filename="Dota_V7.8.w3x"md5="90498B2406383B72B29D18B4DF62DC37"/>
<mapmapid="2"name="Dota版本v9.w3x"filename="Dota_V1.8.w3x"md5="91498B2406383B72B29D18B4DF62DC37"/>
<mapmapid="3"name="Dota版本v11.w3x"filename="Dota_V2.8.w3x"md5="91498B2406383B72B29D18B4DF62DC37"/></savemaplist>

读写代码


void				writeMapXmlData(){
	//savewhenquit
	TiXmlDocumentxmlDoc;
	TiXmlDeclarationDeclaration("1.0","utf-8","yes");
	xmlDoc.InsertEndChild(Declaration);

	TiXmlNode*pNode=NULL;
	TiXmlElement*pRootElm=newTiXmlElement("savemaplist");
	pNode=xmlDoc.InsertEndChild(*pRootElm);
	pRootElm=pNode->ToElement();

	TiXmlElement*pChildElm=newTiXmlElement("map");
	for(inti=0;i<m_rpgMapData_.size();++i){
		std::stringu8node;
		pChildElm->SetAttribute("mapid",m_rpgMapData_.at(i).mapid);
		common::Base_W2U8(m_rpgMapData_.at(i).showmapname,u8node);
		pChildElm->SetAttribute("name",u8node.c_str());
		common::Base_W2U8(m_rpgMapData_.at(i).savename,u8node);
		pChildElm->SetAttribute("filename",u8node.c_str());
		pChildElm->SetAttribute("md5",m_rpgMapData_.at(i).md5.c_str());
		pNode=pRootElm->InsertEndChild(*pChildElm);
	}

	xmlDoc.SaveFile(L"configrpgmapd.xml");}
void				initReadRpgMapXmlData(){
	m_rpgMapData_.clear();
	std::vector<rpgMapData>().swap(m_rpgMapData_);
	//first,readxmlfile
	TiXmlDocumentxmlStr;
	if(FALSE==xmlStr.LoadFile(_TEXT("configrpgmapd.xml"),TIXML_ENCODING_UTF8))
	{
		QB_error("公告信息格式错误");
		return;
	}
	//	m_vecItem.clear();
	TiXmlElement*root=xmlStr.RootElement();//TeamNewsroot
	if(root)
	{
		TiXmlElement*firstNews=root->FirstChildElement();
		while(firstNews){
			rpgMapDataitem;
			std::stringfilename;
			filename=firstNews->Attribute("mapid");
			item.mapid	=atoi(filename.c_str());
			filename=firstNews->Attribute("name");
			common::Base_U82W(filename,item.showmapname);
			filename=firstNews->Attribute("filename");
			common::Base_U82W(filename,item.savename);
			item.md5=firstNews->Attribute("md5");
			item.haddownload=false;
			m_rpgMapData_.push_back(item);
			firstNews=firstNews->NextSiblingElement();
		}
	}
	}

一些所需的类型

structrpgMapData{ intmapid; std::stringmd5; std::wstringshowmapname; //显示的名称 std::wstring savename; //存盘名称 boolhaddownload; rpgMapData(){ mapid=0; md5=""; showmapname=L""; haddownload=false; } rpgMapData(intv):mapid(v){ md5=""; showmapname=L""; haddownload=false; } booloperator==(constrpgMapData&_R)const{ returnmapid==_R.mapid; }};


tinyxml读写xml示例代码

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读