读xml生成代码例子

读xml生成相应的 lua解析协议代码:

#include <iostream>#include "tinyxml2.h"#include <fstream>#include <string>using namespace tinyxml2;using namespace std;std::ofstream file("readProto.lua",std::ios::ate|std::ios::binary);void read_S_ElementChild(XMLElement *surface){while (surface){const XMLAttribute *attr = surface->FirstAttribute();//获取第一个属性值string strType;bool isArray = false;while(attr){if (strcmp(attr->Name(), "name")==0){string name = attr->Value();string dataStr = "data.";string temp1 = "\t" + dataStr + name + " = ";string temp;if(strcmp(strType.c_str(), "String")==0){temp = "is:readUTF8()";}else if(strcmp(strType.c_str(), "long")==0){temp = "is:readLong()";}else if(strcmp(strType.c_str(), "int")==0){temp = "is:readInt()";}else if(strcmp(strType.c_str(), "short")==0){temp = "is:readShort()";}else if(strcmp(strType.c_str(), "byte")==0){temp = "is:readByte()";}else if(strcmp(strType.c_str(), "boolean")==0){temp = "is:readBool()";}else if(strcmp(strType.c_str(), "float")==0){temp = "is:readJavaFloat()";}else{size_t len = strType.size();string lastStr = strType.substr(len-2, 2);if(strcmp(lastStr.c_str(), "[]")==0){ //代表是数组isArray= true;file << "\tlocal cnt = is:readShort()" << endl;file << "\tlocal temps = {}" << endl;file << "\tfor i=1, cnt do" << endl;string preStr = strType.substr(0, len-2);string preStrTemp;if(strcmp(preStr.c_str(), "String")==0){preStrTemp = "is:readUTF8()";}else if(strcmp(preStr.c_str(), "long")==0){preStrTemp = "is:readLong()";}else if(strcmp(preStr.c_str(), "int")==0){preStrTemp = "is:readInt()";}else if(strcmp(preStr.c_str(), "short")==0){preStrTemp = "is:readShort()";}else if(strcmp(preStr.c_str(), "byte")==0){preStrTemp = "is:readByte()";}else if(strcmp(preStr.c_str(), "boolean")==0){preStrTemp = "is:readBool()";}else if(strcmp(preStr.c_str(), "float")==0){preStrTemp = "is:readJavaFloat()";}else{preStrTemp = "read_" + preStr + "(is)";}file << "\t\ttemps[i] = " << preStrTemp << endl;file << "\tend" << endl;}else{temp = "read_" + strType + "(is)";}}if(!isArray){file << temp1 + temp;}else{ //是数组file << temp1 + "temps" ;}}else if(strcmp(attr->Name(), "type")==0){strType = attr->Value();}else if(strcmp(attr->Name(), "description")==0){string desc = attr->Value();string temp = " –" + desc;file << temp << endl;if(isArray){file << endl;}}attr = attr->Next(); //获取下一个属性值}surface = surface->NextSiblingElement();//当前对象的下一个节点}}void read_C_ElementChild(XMLElement *surface){int i = 0;while (surface){string strType;const XMLAttribute *attr = surface->FirstAttribute();//获取第一个属性值while(attr){if (strcmp(attr->Name(), "name")==0){string temp;bool isNum = false;string param = "param";char num[4]="";sprintf_s(num, "%d", i);if(strcmp(strType.c_str(), "String")==0){temp = "sendData:writeUTF8(";}else if(strcmp(strType.c_str(), "long")==0){isNum = true;temp = "sendData:writeLong(";}else if(strcmp(strType.c_str(), "int")==0){isNum = true;temp = "sendData:writeInt(";}else if(strcmp(strType.c_str(), "short")==0){isNum = true;temp = "sendData:writeShort(";}else if(strcmp(strType.c_str(), "byte")==0){isNum = true;temp = "sendData:writeByte(";}else if(strcmp(strType.c_str(), "boolean")==0){temp = "sendData:writeBool(";}else if(strcmp(strType.c_str(), "float")==0){isNum = true;temp = "sendData:writeJavaFloat(";}else{temp = "error ############ ";}if (isNum){file << "\tlocal " + param + num + " = tonumber(" + "self:getExtraValue(" + num + "))" << endl;}else{file << "\tlocal " + param + num + " = " + "self:getExtraValue(" + num + ")" << endl;}file << "\t" + temp + param + num + ")" ;}else if(strcmp(attr->Name(), "type")==0){strType = attr->Value();}else if(strcmp(attr->Name(), "description")==0){file << " –" << attr->Value() << endl;}attr = attr->Next(); //获取下一个属性值}i = i + 1;surface = surface->NextSiblingElement();//当前对象的下一个节点}}void read_xml(XMLElement *surface){while (surface){string id;string name;bool isRead = true;const XMLAttribute *attr = surface->FirstAttribute();//获取第一个属性值while(attr){if(strcmp(attr->Name(), "id")==0){id = attr->Value();}else if(strcmp(attr->Name(), "name")==0){name = attr->Value();string fristStr = name.substr(0,1);string wirteStr = "function read_" + name + "(is)";if(strcmp(fristStr.c_str(), "C")== 0 ){isRead = false;wirteStr = "function send_" + name + "(self)";}file << wirteStr ;}else if(strcmp(attr->Name(), "description")==0){file << " –" << attr->Value() << endl;if (!isRead){file << "\tlocal " + name + " = " + id + " –" + attr->Value() << endl;file << "\tlocal sendData = ByteArrayOutputStream()" << endl;file << "\tsendData:writeInt(" + id + ")" << endl;}}attr = attr->Next(); //获取下一个属性值}XMLElement *surface1 = surface->FirstChildElement(); //查看当前对象是否有子节点if(surface1){if(isRead){file << "\tlocal data = {}" << endl;read_S_ElementChild(surface1);//递归调用}else{read_C_ElementChild(surface1);}}if (isRead){file << "\treturn data" << endl;}else{file << "\tsendDataStream(sendData)" << endl;}file << "end" << endl;file << endl;surface = surface->NextSiblingElement();//当前对象的下一个节点}}int main(){if(!file){std::cout<<"文件打开失败!";abort();//等同于exit}tinyxml2::XMLDocument mydocument; //声明xml对象mydocument.LoadFile("protocol.xml"); //载入xml文件XMLElement *rootElement = mydocument.RootElement(); //获取跟节点XMLElement *surface = rootElement->FirstChildElement("message");//获取第一个值为value的子节点 默认为空则返回第一个节点read_xml(surface);file.close();cin.get();}

歌里唱的是“你离开我,就是旅行的意义”,

读xml生成代码例子

相关文章:

你感兴趣的文章:

标签云: