Read xml to generate code example, xml code example

Source: Internet
Author: User

Read xml to generate code example, xml code example

Read xmlGenerateCorrespondingLua parsing protocol code:

# 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 (); // obtain the first attribute value string strType; bool isArray = false; while (attr) {if (strcmp (attr-> Name (), "name") = 0) {stri Ng 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) {// indicates the array 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 (st Rcmp (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 {// is an array 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 (); // obtain the Next property value} surface = surface-> NextSiblingElement (); // The Next node of the current object} void read_C_ElementChild (XMLE Lement * surface) {int I = 0; while (surface) {string strType; const XMLAttribute * attr = surface-> FirstAttribute (); // get the first attribute value 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 = "s EndData: 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 (); // obtain the Next property value} I = I + 1; surface = surface-> NextSiblingElement (); // next node of the current object} void read_xml (XMLElement * surface) {while (surface) {string id; string name; bool isRead = true; const XMLAttribute * attr = surface-> FirstAttribute (); // obtain the first attribute value while (attr) {if (strcmp (attr-> Name (), "id ") = 0) {id = attr-> Value ();} el Se 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 (); // obtain the Next property value} XMLElement * surface1 = surface-> FirstChildElement (); // check whether the current object has a subnode if (surface1) {if (isRead) {file <"\ tlocal data = {}" <endl; read_S_ElementChild (surface1); // recursive call} else {read_C_E LementChild (surface1) ;}}if (isRead) {file <"\ treturn data" <endl ;}else {file <"\ tsendDataStream (sendData) "<endl ;}file <" end "<endl; file <endl; surface = surface-> NextSiblingElement (); // The next node of the current object} int main () {if (! File) {std: cout <"file opening failed! "; Abort (); // equivalent to exit} tinyxml2: XMLDocument mydocument; // declare the xml Object mydocument. loadFile ("protocol. xml "); // load the xml file XMLElement * rootElement = mydocument. rootElement (); // get the following node XMLElement * surface = rootElement-> FirstChildElement ("message "); // get the first value of the child node is empty by default, return the first node read_xml (surface); file. close (); cin. get ();}


Xml

<Message id = "0x09001017" name = "S_ITEM_ADD_TASK" description = "add a task item to the backpack"> <field type = "int" name = "task_max" description = "current length of the task item package (space may exist) "/> <field type =" S_ITEM_TASKPROPS [] "name =" items "description =" data "/> </message>

<Message id = "0x02000006" name = "C_MAP_NODE_ARRIVE" description = "role arrival node"> <field type = "int" name = "id" description = "level node id "/> <field type =" boolean "name =" isEnd "description =" reached "/> </message>

Effect:

Lua

Function read_S_ITEM_ADD_TASK (is) -- add a task item local data = {} data to the backpack. task_max = is: readInt () -- current length of the task item package (may have spaces) local cnt = is: readShort () local temps = {} for I = 1, cnt dotemps [I] = read_S_ITEM_TASKPROPS (is) enddata. items = temps -- Data return dataend

Function send_C_MAP_NODE_ARRIVE (self) -- Role arrival node local C_MAP_NODE_ARRIVE = 0x02000006 -- Role arrival node local sendData = ByteArrayOutputStream () sendData: writeInt (0x02000006) local param0 = tonumber (self: getExtraValue (0) sendData: writeInt (param0) -- level node idlocal param1 = self: getExtraValue (1) sendData: writeBool (param1) -- whether to reach the end of sendDataStream (sendData) end

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.