读xml生成代码例子(二)

2015-07-24 10:52:21 · 作者: · 浏览: 8
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

	
		
		
	

	
		
		
	

生成效果:

lua

function read_S_ITEM_ADD_TASK(is)  --向背包添加一种任务道具
	local data = {}
	data.task_max = is:readInt()  --任务道具包裹的当前长度(可能有空格)
	local cnt = is:readShort()
	local temps = {}
	for i=1, cnt do
		temps[i] = read_S_ITEM_TASKPROPS(is)
	end
	data.items = temps  --数据

	return data
end

function send_C_MAP_NODE_ARRIVE(self)  --角色到达结点
	local C_MAP_NODE_ARRIVE = 0x02000006  --角色到达结点
	local sendData = ByteArrayOutputStream()
	sendData:writeInt(0x02000006)
	local param0 = tonumber(self:getExtraValue(0))
	sendData:writeInt(param0)  --关卡结点id
	local param1 = self:getExtraValue(1)
	sendData:writeBool(param1)  --是否到达终点
	sendDataStream(sendData)
end