读取xml生成lua測试代码
#include <iostream>
#include <string>
#include <fstream>
#include "tinyxml2.h"
using namespace std;
using namespace tinyxml2; std::ofstream file("test.lua",std::ios::ate|std::ios::binary); void read_ElementChild(XMLElement *surface)
{
string punStr = ","; //标点符号
file << "\t\t";
bool isArray = true; //是否是循环
while (surface) {
const XMLAttribute *attr = surface->FirstAttribute();
string strType; while (attr) {
if ( strcmp(attr->Name(), "name") == 0 ){
string name = attr->Value();
string preStr = name + " = ";
string preStr1 = name + " = { ";
string temp; if ( strcmp(strType.c_str(), "String") == 0 ){
isArray = false;
temp = "\"呵呵00001\"" + punStr;
}else if(strcmp(strType.c_str(), "long")==0){
isArray = false;
temp = "100" + punStr;
}else if(strcmp(strType.c_str(), "int")==0){
isArray = false;
temp = "10" + punStr;
}else if(strcmp(strType.c_str(), "short")==0){
isArray = false;
temp = "10" + punStr;
}else if(strcmp(strType.c_str(), "byte")==0){
isArray = false;
temp = "2" + punStr;
}else if(strcmp(strType.c_str(), "boolean")==0){
isArray = false;
temp = "true" + punStr;
}else if(strcmp(strType.c_str(), "float")==0){
isArray = false;
temp = "2" + punStr;
}else{
if (!isArray){ //假设上次不是循环 这次是循环 换一行 并加入两个tab键空格
file << endl;
file << "\t\t" ;
}
isArray = true;
size_t len = strType.size();
string lastStr = strType.substr(len-2, 2);
if ( strcmp(lastStr.c_str(), "[]" ) == 0 ){
string preStr = strType.substr(0, len-2);
if ( strcmp(preStr.c_str(), "String") == 0 ){
temp = "\"呵呵00001\"" + punStr + " }" + punStr;
}else if( strcmp(preStr.c_str(), "long")==0){
temp = "100" + punStr + " }" + punStr;
}else if( strcmp(preStr.c_str(), "int")==0){
temp = "10" + punStr + " }" + punStr;
}else if( strcmp(preStr.c_str(), "short")==0){
temp = "10" + punStr + " }" + punStr;
}else if( strcmp(preStr.c_str(), "byte")==0){
temp = "2" + punStr + " }" + punStr;
}else if( strcmp(preStr.c_str(), "boolean")==0){
temp = "true" + punStr + " }" + punStr;
}else if( strcmp(preStr.c_str(), "float")==0){
temp = "2" + punStr + " }" + punStr;
}else {
temp = "{ read_" + preStr + " }, }" + punStr;
} temp = "[1]=" + temp; //数组加入[1]
}else{ //处理不是数组 读取别的协议 temp = " read_" + strType + " }" + punStr;
}
} if (isArray) {
file << preStr1 + temp;
}else {
file << preStr + temp;
}
}else if ( strcmp(attr->Name(), "type") == 0 ){
strType = attr->Value();
} attr = attr->Next(); //下一个属性
} surface = surface->NextSiblingElement(); //下一个节点
if (isArray && surface != NULL) //处理最后一行假设是数组 不空出一行
{
file << endl;
file << "\t\t";
}
}
file << endl;
file << "\t}" << endl; } void read_xml(XMLElement *surface)
{
while (surface) {
const XMLAttribute *attr = surface->FirstAttribute();
string name;
bool isRead = true; //假设是 C_ 协议就不继续读取了
while (attr) {
if ( strcmp(attr->Name(), "name") == 0 )
{
name = attr->Value();
string fristStr = name.substr(0,1);
string writeStr;
if ( strcmp(fristStr.c_str(), "S") == 0 )
{
writeStr = "function read_" + name + "()";
}else{
isRead = false;
break;
}
file << writeStr;
}else if ( strcmp(attr->Name(), "description") == 0 )
{
file << " --" << attr->Value() << endl;
}
attr = attr->Next();
} if (! isRead) {//假设是 C_ 协议就不继续读取了
surface = surface->NextSiblingElement();
continue;
} XMLElement *surface1 = surface->FirstChildElement(); //读取子节点
if (surface1){
file << "\tlocal data = {" << endl;
read_ElementChild(surface1);
} file << "end" << endl;
file << endl;
surface = surface->NextSiblingElement();
}
} int main(int argc, const char * argv[])
{
tinyxml2::XMLDocument myDocument;
myDocument.LoadFile("protocol.xml");
XMLElement *rootElement = myDocument.RootElement();
XMLElement *surface = rootElement->FirstChildElement("message");
read_xml(surface); return 0;
}
xml測试数据
<project name="Protocol">
<message id="0x42001003" name="S_CROSS_ARENA_UI" description="跨服争霸场角色信息">
<field type="S_CROSS_ARENA_ROLE_INFO[]" name="roleList" description="角色列表"/>
<field type="S_CROSS_ROLE_TOP_THREE_INFO[]" name="topThreeRoles" description="前三名"/>
<field type="int" name="fightCap" description="战斗力"/>
<field type="int" name="rankNo" description="排名"/>
<field type="int" name="leftAttackCount" description="剩余战斗攻击次数"/>
<field type="int" name="leftFreeResetCount" description="剩余免费重置次数"/>
<field type="long" name="resetExpend" description="添加战斗次数消耗钻石数"/>
<field type="long" name="addAttackExpend" description="添加战斗次数消耗钻石数"/>
<field type="S_CROSS_ROLE_TOP_THREE" name="test" description="消耗"/>
<field type="int" name="score" description="积分"/>
<field type="boolean[]" name="gradeRewards" description="三个档次的奖励是否可领取"/>
</message>
</project>
生成lua
function read_S_CROSS_ARENA_UI(self) --跨服争霸场角色信息
local data = {
roleList = { [1]={ read_S_CROSS_ARENA_ROLE_INFO }, },
topThreeRoles = { [1]={ read_S_CROSS_ROLE_TOP_THREE_INFO }, },
fightCap = 10,rankNo = 10,leftAttackCount = 10,leftFreeResetCount = 10,resetExpend = 100,addAttackExpend = 100,
test = { read_S_CROSS_ROLE_TOP_THREE },
score = 10,
gradeRewards = { [1]=true, },
}
end
使用方法。用里面的标示,找到其它的协议把里面的内容复制过来.
也能够用函数返回方式。生成代码这样:
function read_S_CROSS_ARENA_UI() --跨服争霸场角色信息
local data = {
roleList = { [1]= read_S_CROSS_ARENA_ROLE_INFO(), },
topThreeRoles = { [1]= read_S_CROSS_ROLE_TOP_THREE_INFO(), },
fightCap = 10,rankNo = 10,leftAttackCount = 10,leftFreeResetCount = 10,resetExpend = 100,addAttackExpend = 100,
test = read_S_CROSS_ROLE_TOP_THREE(),
score = 10,
gradeRewards = { [1]=true, },
}
return data
end
读取xml生成lua測试代码的更多相关文章
- Android网络传输中必用的两个加密算法:MD5 和 RSA (附java完毕測试代码)
MD5和RSA是网络传输中最经常使用的两个算法,了解这两个算法原理后就能大致知道加密是怎么一回事了.但这两种算法使用环境有差异,刚好互补. 一.MD5算法 首先MD5是不可逆的,仅仅能加密而不能解密. ...
- maven多module项目中千万不要引入其它模块的单元測试代码
本文出处:http://blog.csdn.net/chaijunkun/article/details/35796335,转载请注明. 因为本人不定期会整理相关博文,会对对应内容作出完好. 因此强烈 ...
- 基于redis集群实现的分布式锁,可用于秒杀商品的库存数量管理,有測试代码(何志雄)
转载请标明出处. 在分布式系统中,常常会出现须要竞争同一资源的情况,本代码基于redis3.0.1+jedis2.7.1实现了分布式锁. redis集群的搭建,请见我的另外一篇文章:<>& ...
- Tensorflow MNIST 数据集測试代码入门
本系列文章由 @yhl_leo 出品,转载请注明出处. 文章链接: http://blog.csdn.net/yhl_leo/article/details/50614444 測试代码已上传至GitH ...
- OpenFace库(Tadas Baltrusaitis)中基于Haar Cascade Classifiers进行人脸检測的測试代码
Tadas Baltrusaitis的OpenFace是一个开源的面部行为分析工具.它的源代码能够从 https://github.com/TadasBaltrusaitis/OpenFace 下载. ...
- DIV旋转的測试代码
<html> <head> <style type="text/css"> .rat0 { -webkit-transform: rotate( ...
- Testng 的数据源 驱动測试 代码与配置
JUnit中有讲述使用注解的方式进行数据源读取进行自己主动循环測试的方法,在TestNG中也提供了对应的方法 public class TestngDataProvider { /** * 数组内的每 ...
- 基于webrtc的媒体库測试代码以及接口介绍
经过一段时间的项目验证,第一版接口能够定版了.满足一般的项目需求是没有问题了,接口非常清晰,凝视也写的非常清晰,大家有须要的就拿去測试吧,android版本号还在验证中.假设有什么问题或者须要源码.能 ...
- MongoDB之Java測试代码(DAO层)
MongoInit.java是数据库初始化及连接类 MongoUtils.java是对mongodb的各种操作方法 MongoInit.java package com.wlwcloud.datate ...
随机推荐
- Python Hashlib笔记
#python3.4hashlib module - A common interface to many hash functions.hash.digest() - Return the dige ...
- Android目录结构
|ABI-- 应用程序二进制接口(application binary interface,ABI) |-- Makefile |-- bionic (bionic C库) ...
- php删除
<?php$id = $_GET['id'];$db= new Mysqli("localhost","root","root",&q ...
- 在ubuntu上安装npm
sudo apt install npm 执行上面的安装命令,会报错: Reading package lists... Done Building dependency tree Reading s ...
- 九度oj 题目1108:堆栈的使用
题目描述: 堆栈是一种基本的数据结构.堆栈具有两种基本操作方式,push 和 pop.Push一个值会将其压入栈顶,而 pop 则会将栈顶的值弹出.现在我们就来验证一下堆栈的使用. 输入: 对于每组测 ...
- Replication and Triggers
参考官网:https://dev.mysql.com/doc/refman/5.7/en/replication-features-triggers.html 需要了解复制和触发器关系的背景: 程序变 ...
- LightOJ1106 Gone Fishing
Gone Fishing John is going on a fishing trip. He has h hours available, and there are n lakes in the ...
- Fruit Ninja
Fruit Ninja 时间限制:C/C++ 5秒,其他语言10秒 空间限制:C/C++ 262144K,其他语言524288K 64bit IO Format: %lld 题目描述 Fruit Ni ...
- 转 DOS 8.3 文件名命名规则
http://www.360doc.com/content/10/0813/14/73007_45757514.shtml DOS 8.3 文件名命名规则 经常看到命令行或者其它软件在显示目录的时候出 ...
- python模块(二)
一.json模块 作用: 用于[字符串]和 [python基本数据类型] 间进行转换 Python的Json模块序列化与反序列化的过程分别是 encoding和 decoding. encoding ...