xml格式解析

28 篇文章 0 订阅
	//printf("项目中的xml解析\n");
	//得到路径
	const char *path = lua_tostring(L, 1);
	//得到 最后的表明
	const char *tableKey = lua_tostring(L, 2);

	lua_getglobal(L, "cc");

	lua_getfield(L, -1, "GameArgs");
	//把路径转为字符流
	std::string buffer = CCFileUtils::getInstance()->getStringFromFile(path);
	tinyxml2::XMLDocument *doc = new tinyxml2::XMLDocument();
	doc->Parse(buffer.c_str());
	tinyxml2::XMLElement *root = doc->RootElement();

	lua_newtable(L);

	tinyxml2::XMLElement *data = root->FirstChildElement();
	// 7 具体的value 6 具体的key 5 table 4 key 3 table(value) 2 GameArgs 1 cc
	while (data)
	{
		//firstEle
		tinyxml2::XMLElement *firstEle = data->FirstChildElement();

		//key
		lua_pushnumber(L, atoi(firstEle->FirstChild()->Value()));
		lua_newtable(L);

		//<vipLevel type="int" multiLanguage="0">0</vipLevel>
		while (firstEle)
		{
			//firstEle
			tinyxml2::XMLNode *node = firstEle->FirstChild();

			//key
			lua_pushstring(L, firstEle->Value());
			// Value
			//lua_pushstring(L, node->Value());
			//printf("aaa = %s %s\n", firstEle->Value(), node->Value());
			const tinyxml2::XMLAttribute *attr = firstEle->FirstAttribute();
			while (attr)
			{
				if (strcmp(attr->Name(), "type") == 0 && strcmp(attr->Value(), "int") == 0)
				{
					lua_pushinteger(L, atoi(node->Value()));
				}
				else if ((strcmp(attr->Name(), "type") == 0 && strcmp(attr->Value(), "double") == 0))
				{
					lua_pushnumber(L, atof(node->Value()));
				}
				else if ((strcmp(attr->Name(), "type") == 0 && strcmp(attr->Value(), "string") == 0) ||
					(strcmp(attr->Name(), "type") == 0 && strcmp(attr->Value(), "String") == 0)
					)
				{
					// 多国语言
					if (strcmp(attr->Next()->Name(), "multiLanguage") == 0 && strcmp(attr->Next()->Value(), "1") == 0)
					{
						std::string str = "return ";
						str += node->Value();
						lua_pushstring(L, str.c_str());
					}
					else if (strcmp(attr->Next()->Name(), "multiLanguage") == 0 && strcmp(attr->Next()->Value(), "0") == 0)
					{
						char buf[1024];
						sprintf(buf, "return \"%s\"", node->Value());
						lua_pushstring(L, buf);
					}
					/*lua_pushstring(L, node->Value());*/
				}
				attr = attr->Next();
			}
			lua_settable(L, -3);

			firstEle = firstEle->NextSiblingElement();
		}
		lua_settable(L, -3);
		data = data->NextSiblingElement();
	}
	lua_setfield(L, -2, tableKey);//t[k] = v
	delete doc;

	return 1;
}

	printf("lolXMLParse解析\n");
	//lolRes/lol.xml
	const char *path = lua_tostring(L, -1);
	//拿到xml解析指针
	tinyxml2::XMLDocument *doc = new tinyxml2::XMLDocument();
	//将文件转为字符流
	std::string buf = CCFileUtils::getInstance()->getStringFromFile(path);
	doc->Parse(buf.c_str());
	//得到root
	tinyxml2::XMLElement *root = doc->RootElement();

	lua_newtable(L);
	//栈 table	3 table2   2 id(key)   1 table

	//得到data
	tinyxml2::XMLElement *data = root->FirstChildElement();
	while (data)
	{
		//得到第一行ele
		tinyxml2::XMLElement *firstEle = data->FirstChildElement();

		lua_pushnumber(L, atoi(firstEle->FirstChild()->Value()));
		lua_newtable(L);

		while (firstEle)
		{
			tinyxml2::XMLNode *node = firstEle->FirstChild();
			//printf("heroId = %s idNum = %s\n", firstEle->Value(), node->Value());
			//key
			lua_pushstring(L, firstEle->Value());
			//value
			lua_pushstring(L, node->Value());
			lua_settable(L, -3);
			firstEle = firstEle->NextSiblingElement();
		}
		lua_settable(L, -3);
		data = data->NextSiblingElement();
	}

	return 1;





  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值