Summary of the invention
In view of this, fundamental purpose of the present invention is to provide method and the device of a kind of XML message and the conversion of C programmer structure, automatically finishes the conversion of XML message and C programmer structure, raises the efficiency.
For achieving the above object, technical scheme of the present invention is achieved in that
The method of a kind of XML message and C programmer structure conversion arranges the corresponding relation of XML message and C programmer structure form, and the method comprises:
A1, reception also parse field information and C programmer structure form that described XML message is corresponding from XML message; According to described field information and C programmer structure form, set up corresponding C programmer structure;
Perhaps,
A2, according to C programmer structure and described corresponding relation, set up the XML message corresponding with this C programmer structure;
Wherein, according to described field information and C programmer structure form, set up corresponding C programmer structure and specifically comprise:
A11, according to described field information and C programmer structure form corresponding to described XML message, set up the C programmer structure node of current Related fields;
Whether A12, the current field of analysis comprise lower one deck field, if so, set up the C programmer structure node of lower one deck Related fields of current field; Set up until finish the C programmer structure node of the N layer field of current field;
A13, set up the C programmer structure node of the next Related fields of XML message, set up until finish the C programmer structure node of all Related fields of XML message;
Wherein, described according to C programmer structure and described corresponding relation, the method for setting up the XML message corresponding with this C programmer structure is:
Each field of XML message is considered as character string, corresponding relation according to C programmer structure and this structure and XML message, namely obtain node level relational tree, the data type of each node, the position of each node in internal memory of C programmer structure, data in the structure internal memory are write each field of XML message, thereby set up corresponding XML message.
Described XML message is corresponding one by one with C programmer structure form.
The node type of the field type of described XML message or C programmer structure comprises: integer, character type, digit group type or structure.
The device of a kind of XML message and the conversion of C programmer structure, this device comprises:
The first module is for the corresponding relation that XML message and C programmer structure form are set;
The second module is used for receiving and parsing field information and C programmer structure form corresponding to described XML message from XML message;
The 3rd module is used for according to described field information and C programmer structure form, sets up corresponding C programmer structure;
Four module is used for according to C programmer structure and described corresponding relation, sets up the XML message corresponding with this C programmer structure;
Wherein, described the 3rd module comprises:
First module is used for according to described field information and C programmer structure form corresponding to described XML message, sets up the C programmer structure node of current Related fields;
Second unit is used for analyzing current field and whether comprises lower one deck field, if so, sets up the C programmer structure node of lower one deck Related fields of current field; Set up until finish the C programmer structure node of the N layer field of current field;
Unit the 3rd is used for setting up the C programmer structure node of the next Related fields of XML message, sets up until finish the C programmer structure node of all Related fields of XML message;
Wherein, described four module comprises:
Unit the 4th, be used for each field of XML message is considered as character string, corresponding relation according to C programmer structure and this structure and XML message, namely obtain node level relational tree, the data type of each node, the position of each node in internal memory of C programmer structure, data in the structure internal memory are write each field of XML message, thereby set up corresponding XML message.
The present invention sets in advance corresponding relation between XML message and the C programmer structure form according to user's request, during communication, automatically finish the parsing of XML message, and so that XML message can map directly in the corresponding C programmer structure internal memory, simplify conversion work, thereby so that the C programmer easier use XML message of energy and external program, particularly the web program is carried out alternately.On the other hand, according to the information of C programmer structure node, also can directly be converted to corresponding XML message.When the XML of needs message format changed, the corresponding relation that upgrades between XML message and the C programmer structure form can satisfy new demand, strong adaptability.
Embodiment
The present invention is by setting in advance the corresponding relation of XML message and C programmer structure form, the field information that the XML message parse is gone out is automatically converted to the structure node description that C programmer can be identified, and perhaps C programmer structure nodal information is converted to XML message; This conversion is finished automatically, has avoided the various mistakes of artificial conversion, simultaneously, can increase the corresponding relation of XML message and C programmer structure form according to user's needs.
Below by specific embodiment and description of drawings technical scheme of the present invention.
Method flow of the present invention as shown in Figure 1, the corresponding relation of XML message and C programmer structure form is set, wherein: the value of the field name in the XML message, field type and each field is mapped as the hierarchical relationship between nodename, node type, node side-play amount and each node of C programmer structure.The method comprises two parts: the XML message conversion is the C programmer structure, or the C programmer structure is converted to XML message.
Step 101, reception also parse field information and C programmer structure form corresponding to described XML message from XML message;
XML message for example:
Wherein<p〉</p〉field of expression and value thereof,<sp〉</sp〉represent it is a structure,<ap〉</ap〉represent it is an array.The present invention only relates to parsing and the generation of the XML message of C programmer and extraneous program usefulness XML interacting message.If consider the transmission needs of network program, such as the TCP transmission, also to add some information in the front of XML message, such as message overall length, type of message etc., correctly receive XML message and processing for C programmer.These information formats and content be not in the scope of the invention.The numbering of C programmer structure form corresponding to XML message is set in the reserved place of XML message, like this, by this numbering, determines corresponding C programmer structure form.
Each field information comprises field name, the value of field type and field, XML message in the upper example can not directly be identified by C programmer, need at first resolve C programmer structure corresponding to this XML message, and then the field information that comprises of Analysis of X ML message, each field has a C programmer structure node corresponding with it, as shown in Figure 2, the C programmer structure name corresponding with this XML message is called c_java_firstmsg_info_t, comprise 5 ground floor nodes: first nodename Int32Element, type is integer (int), and side-play amount calculates according to size and the c_java_firstmsg_info_t structure memory start address reference position of Int32Element; Second nodename is Int32ElementArray, type is array (Array), and side-play amount is according to calculating gained, and this node comprises two next node layers, be the second layer node of c_java_firstmsg_info_t structure: title is respectively 1 and 2, and type is int; The 3rd nodename of the ground floor of c_java_firstmsg_info_t structure is Int8ElementArray, and type is character string (string), and the maximum length of character string was 4 (comprising '/0 '), and side-play amount is according to calculating gained; The 4th nodename of the ground floor of c_java_firstmsg_info_t structure is StructElement, and type is structure, comprises two second layer nodes: first nodename is Uint32Element, and type is int; Second nodename is Uint8Element, type string, and the character string maximum length was 11 (comprising ' 0/ '), side-play amount is all by calculating; The 5th nodename of the ground floor of c_java_firstmsg_info_t structure is SturctElementArray, type is array, comprise two second layer nodes: first nodename is 1, type is structure, this node comprises two the 3rd node layers: first nodename is Int8Element, and type is array, and this array comprises two the 4th node layers, title is respectively 1 and 2, and type is int; Second nodename of second layer node structure body 1 is Int32Element, and type is character string; Second second layer nodename of SturctElementArray array is 2, and type is structure, and this node comprises two the 3rd node layers: first nodename is Int8Element, type is array, this array comprises two the 4th node layers, and title is respectively 1 and 2, and type is int; Second nodename of second layer node structure body 2 is Int32Element, and type is character string; The calculating of the side-play amount of each node layer is similar to common side-play amount account form in the C programmer, and this repeats no more.
Step 102, according to described field information and C programmer structure form, set up corresponding C programmer structure.
The C programmer structure name corresponding with the field information of XML message reaction is called c_java_firstmsg_info_t, is specially:
The hierarchical relationship that embodies in this structure is:
According to described field information and C programmer structure form corresponding to described XML message, set up the C programmer structure node of current Related fields;
Analyze current field and whether comprise lower one deck field, if so, set up the C programmer structure node of lower one deck Related fields of current field; Set up until finish the C programmer structure node of the N layer field of current field;
First field does not contain lower one deck field, so continue to set up second node of c_java_firstmsg_info_t;
Five nodes of the 3rd node to the set up process and the first two node is similar, and are specific as follows:
Can see, when setting up this hierarchical relationship, each field there are its title (with identical in the XML message), data type (such as XMLAGT_TYPE_INT, XMLAGT_TYPE_STRING, XMLAGT_TYPE_ARRAY, XMLAGT_TYPE_STRUCT) with respect to the skew of structure memory start address, if array type or structure type also will be distributed the node of lower one deck; If character string type then will be write down its maximum length with anti-spilled.Then, by the title c_java_firstmsg_info_t of structure, set up the structure that C programmer shown in Figure 2 can Direct Recognition with the hierarchical relationship of each node.
During use, call the such interface of similar ParseCall (char*pBuf, int bufLen, void * pStruct, int structLen, const char * pStructTypeName), wherein pBuf is XML message; BufLen is the length of XML message; PStruct, the pointer of the structure of this XML message of the correspondence of returning corresponds to example and points to exactly a c_java_firstmsg_info_t structure; StructLen, the size of the structure of corresponding this XML message, corresponding to example is exactly sizeof (c_java_firstmsg_info_t); PStructTypeName, the typonym of the structure of corresponding this XML message, corresponding to example is exactly " c_java_firstmsg_info_t ".
In this ParseCall () interface, just can encapsulate the function in certain C/C++xml storehouse (such as libxml2), untiing of one-level one-level, simultaneously with the name of each field (as<p id=" Int32Element " value=" 1 "/, the name of this field is " Int32Element ", its value is 1) with the hierarchical tree of structure in the title of field be mapped, if the type of this field is not array type or structure type, the value of field is write the side-play amount of this field; If the type of this field is array or structure, so also to continue to solve the value of lower one deck field downwards.
More than be the processing procedure of analyzing XML message, when needs are converted to XML message from the C programmer structure, adopt step 103: according to C programmer structure and described corresponding relation, set up the XML message corresponding with this C programmer structure.
The process that generates XML message is relatively simple, each field of XML message is considered as character string, corresponding relation according to C programmer structure and this structure and XML message, namely obtain node level relational tree, the data type of each node, the position of each node in internal memory of C programmer structure, data in the structure internal memory are write each field of XML message, thereby set up corresponding XML message.
The present invention also provides the device of corresponding XML message and the conversion of C programmer structure, and as shown in Figure 3, this device comprises memory module and execution module, wherein,
Memory module is used for the XML message of storage setting and the corresponding relation of C programmer structure;
It is corresponding C programmer structure that execution module is used for the XML message conversion, perhaps, the C programmer structure is converted to corresponding XML message.
Described execution module comprises parsing module, modular converter and package module, wherein,
Parsing module is used for receiving and parsing field information from XML message;
Modular converter is used for field information and the described corresponding relation according to the XML message of resolving, and sets up corresponding C programmer structure;
Package module is used for according to C programmer structure and described corresponding relation, sets up corresponding XML message.
The above is preferred embodiment of the present invention only, is not for limiting protection scope of the present invention.