//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;