Data format conversion method and device, computer equipment and storage medium
Converting JSON format data into XML node data through predefined enumerations solves the problem of incompatibility between JSON and XML formats, realizes direct interaction between modules and services, and improves the convenience of using software modules.
Patent Information
- Application Number
- CN202410175742.1
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2024-02-07
- Publication Date
- 2024-09-17
AI Technical Summary
The compatibility issues of existing software modules with data in JSON and XML formats result in the inability of services and modules to interact directly, affecting convenience of use.
Convert JSON format data into XML node data through predefined enumerations, and convert it into XML format data according to the hierarchical structure to achieve automatic format conversion between JSON and XML.
It overcomes the problem of incompatibility between JSON and XML formats and allows direct interaction between software modules for JSON format data and services for XML format data and vice versa, improving the convenience of using software modules.
Smart Images

Figure CN118657117A
Abstract
Description
Technical Field
[0001] The present application relates to the field of computer technology, and in particular to a data format conversion method, device, computer equipment and storage medium. Background Art
[0002] With the development of computer technology, various software modules have emerged. However, some software modules are only compatible with specific data formats. For example, the graph editing engine is only compatible with JSON format data.
[0003] However, for software modules that are compatible with JSON format data or XML format data, many services that request access to the software modules use data formats that are different from the software modules, for example, the software module uses JSON format data, while the request service uses XML format data, or the software module uses XML format data, while the request service uses JSON format data, etc. These services and software modules may have data format incompatibility issues, which may result in these services being unable to interact directly with the software modules, affecting the ease of use of the software modules. Summary of the invention
[0004] Based on this, it is necessary to provide a data format conversion method, device, computer equipment and computer-readable storage medium that can improve the ease of use of software modules in response to the above technical problems.
[0005] In a first aspect, the present application provides a data format conversion method. The method comprises:
[0006] According to a predefined enumeration, a first basic structure element in the first JSON format data is converted into XML node data, wherein the predefined enumeration is used to characterize the correspondence between the node name of the first basic structure element and the character encoding of the XML node data; according to the hierarchy of the first JSON format data, each of the XML node data is converted into the first XML format data.
[0007] In one embodiment, converting the first infrastructure element in the first JSON format data into XML node data according to a predefined enumeration includes:
[0008] Detect the type of the node value of the first infrastructure element; if the type of the node value is an object type, construct an XML node of the first infrastructure element according to the predefined enumeration to obtain XML node data; if the type of the node value is a first preset type, convert the first infrastructure element into corresponding XML node data according to the predefined enumeration, wherein the node value of the first preset type is a string, a Boolean value or a number; if the type of the node value is a second preset type, convert the first infrastructure element into an empty node to obtain XML node data, wherein the node value of the first preset type is a meaningless value or an undefined value.
[0009] In one embodiment, constructing an XML node corresponding to the first infrastructure element according to a predefined enumeration includes:
[0010] According to the predefined enumeration and the node name of the first infrastructure element, a start tag of the XML node is constructed; the node attributes of the first infrastructure element are added to the start tag and the preset attribute operation object; according to the predefined enumeration and the node name of the first infrastructure element, an end tag of the XML node is constructed.
[0011] In one embodiment, the adding the node attribute of the first infrastructure element to the start tag and the preset attribute operation object according to the predefined enumeration and the start tag includes:
[0012] Determine defined attributes and undefined attributes other than the start tag in the node attributes of the first infrastructure element; convert the defined attributes into character codes according to the predefined enumeration and add them to the start tag; add the undefined attributes corresponding to the start tag to the start tag, and add the undefined attributes that do not correspond to the start tag to the preset attribute operation object.
[0013] In one embodiment, the method further comprises:
[0014] If there is a child node in the node attribute of the first infrastructure element, then according to the predefined enumeration, a child node tag corresponding to the child node is constructed, and the child node tag is added to the start tag; according to the predefined enumeration, the child attributes of the child node are added to the child node tag and the preset attribute operation object.
[0015] In one embodiment, the method further comprises:
[0016] If the type of the node value is an array type, all array elements in the node value are traversed; each array element is converted into XML node data according to a predefined enumeration; the XML node data of each array element is concatenated to obtain the XML node data corresponding to the first basic structure element.
[0017] In one embodiment, the method further comprises:
[0018] Acquire second XML format data, and convert the second XML format data into a root node element object; generate general JSON format data corresponding to the second XML format data according to the node attributes and node types of the root node element object; convert the general JSON format data into second JOSN format data according to the predefined enumeration.
[0019] In one embodiment, the root node element object includes at least one element node, and the general JSON format data includes a second basic structure element corresponding to the element node; and generating the general JSON format data corresponding to the second XML format data according to the node attribute and node type of the root node element object includes:
[0020] Detect the node type of the element node; if the node type is an instruction node, a text node or a comment node, generate a second infrastructure element corresponding to the element node according to the predefined enumeration and the node value of the element node; if the node type is not an instruction node, a text node or a comment node, traverse the attribute list of the element node to obtain N attribute names and N attribute values of the element node, where N is a positive integer; generate N second infrastructure elements corresponding to the element node according to the N attribute names and the N attribute values.
[0021] In one embodiment, generating N second basic structure elements corresponding to the element node according to the N attribute names and the N attribute values includes:
[0022] Create an empty object and a counter object, and store the attribute name and the attribute value in the form of a key-value pair in the empty object to obtain a target object; if the attribute name is the same as the key name of the existing JSON data, then according to the counter object, add a corresponding counter after the key name corresponding to the attribute name in the target object to obtain a second basic structure element, wherein the counter is used to identify the key name; if the attribute name is not the same as the key name of the existing JSON data, determine the target object as the second basic structure element.
[0023] In a second aspect, the present application also provides a data format conversion device. The device comprises:
[0024] A first conversion module, configured to convert a first basic structure element in the first JSON format data into XML node data according to a predefined enumeration, wherein the predefined enumeration is used to characterize a correspondence between a node name of the first basic structure element and a character encoding of the XML node data;
[0025] The second conversion module is used to convert each of the XML node data into the first XML format data according to the level of the first JSON format data.
[0026] In a third aspect, the present application further provides a computer device. The computer device includes a memory and a processor, the memory stores a computer program, and the processor implements the following steps when executing the computer program:
[0027] According to a predefined enumeration, a first basic structure element in the first JSON format data is converted into XML node data, wherein the predefined enumeration is used to characterize the correspondence between the node name of the first basic structure element and the character encoding of the XML node data; according to the hierarchy of the first JSON format data, each of the XML node data is converted into the first XML format data.
[0028] In a fourth aspect, the present application further provides a computer-readable storage medium. The computer-readable storage medium stores a computer program, and when the computer program is executed by a processor, the following steps are implemented:
[0029] According to a predefined enumeration, a first basic structure element in the first JSON format data is converted into XML node data, wherein the predefined enumeration is used to characterize the correspondence between the node name of the first basic structure element and the character encoding of the XML node data; according to the hierarchy of the first JSON format data, each of the XML node data is converted into the first XML format data.
[0030] In a fifth aspect, the present application further provides a computer program product. The computer program product includes a computer program, and when the computer program is executed by a processor, the following steps are implemented:
[0031] According to a predefined enumeration, a first basic structure element in the first JSON format data is converted into XML node data, wherein the predefined enumeration is used to characterize the correspondence between the node name of the first basic structure element and the character encoding of the XML node data; according to the hierarchy of the first JSON format data, each of the XML node data is converted into the first XML format data.
[0032] The above-mentioned data format conversion method, device, computer equipment and storage medium, since the predefined enumeration can represent the correspondence between the first basic structure element of JOSN format data and the character encoding of XML node data, the first basic structure element in the first JSON format data can be quickly converted into XML node data according to the predefined enumeration, and then according to the hierarchy of the first JSON format data, the XML node data corresponding to each first basic structure element can be converted into the final first XML format data, so that the first JSON format data can be automatically converted into the first XML format data. Based on this, the data format incompatibility problem between the software module using JSON format data and the service using XML format data can be overcome, and the data format incompatibility problem between the software module using XML format data and the service using JSON format data can be overcome. Therefore, the service and software module with incompatibility problems between JSON format data and XML format data can also interact directly, which can improve the convenience of using the software module. BRIEF DESCRIPTION OF THE DRAWINGS
[0033] FIG1 is a schematic diagram of a method for converting JSON format data into XML format data in one embodiment;
[0034] FIG2 is a schematic diagram of a process of converting a first basic structure element into corresponding XML node data in one embodiment;
[0035] FIG3 is a schematic diagram of a visualization process of converting JOSN format data into XML format data in one embodiment;
[0036] FIG4 is a schematic diagram of a method for converting XML format data into JSON format data in one embodiment;
[0037] FIG5 is a schematic diagram of a visualization process of converting XML format data into JSON format data in one embodiment;
[0038] FIG6 is a structural block diagram of a data format conversion device in one embodiment;
[0039] FIG. 7 is a diagram showing the internal structure of a computer device in one embodiment. DETAILED DESCRIPTION
[0040] In order to make the purpose, technical solution and advantages of the present application more clearly understood, the present application is further described in detail below in conjunction with the accompanying drawings and embodiments. It should be understood that the specific embodiments described herein are only used to explain the present application and are not used to limit the present application.
[0041] It should be noted that the data format conversion method in the present application can be applied to software modules that use JSON format data or XML format data to overcome the data incompatibility problem between these software modules and external services that use XML format or JSON format data.
[0042] In one embodiment, as shown in FIG1 , a data format conversion method is provided. This embodiment is illustrated by applying the method to a terminal. It is understandable that the method can also be applied to a server, or to a system including a terminal and a server, and is implemented through the interaction between the terminal and the server. In this embodiment, the method includes the following steps:
[0043] As an example, the above-mentioned software module can be a graph editing engine, such as AntV X6. For the graph editing engine, currently only JSON format data is supported, but there are many services that use XML format data, which makes these services unable to interact directly with the graph editing engine AntV X6, which is very inconvenient and affects the ease of use of the graph editing engine to a certain extent.
[0044] Step 202: Convert a first basic structure element in the first JSON format data into XML node data according to a predefined enumeration, wherein the predefined enumeration is used to characterize the correspondence between the node name of the first basic structure element and the character encoding of the XML node data.
[0045] Among them, the first JSON format data can be JSON format data output by a software module that is only compatible with JSON format data, and in order to enable external services that use XML format data to interact directly with the software module, the present application needs to convert the first JSON format data into XML format data; the first JSON format data can also be JSON format data that an external service request that is only compatible with JSON format data inputs into a software module, and in order to enable a software module that uses XML format data to interact with an external service that is only compatible with JSON format data, the present application needs to convert the first JSON format data into XML format data; the first JSON format data can be composed of multiple first infrastructure elements, which are basic units that constitute the first JSON format data, and can specifically be key-value pairs in the first JSON format data, each key-value pair can be considered as a node in the first JSON format data, the key name of the key-value pair is the node name, and the key value of the key-value pair is the node value; the XML format data can be composed of multiple XML node data, each XML node data represents data representing an XML node; wherein the predefined enumeration is a pre-set mapping relationship set, which is used to characterize the correspondence between the node name in the first infrastructure element and the character encoding in the XML node data, wherein the character encoding is used to characterize the node attributes of the XML node.
[0046] As an example, step 202 includes: according to a predefined enumeration, converting the node names of all first infrastructure elements in the first JSON format data into corresponding character codes, and obtaining XML node data corresponding to all first infrastructure elements, wherein the predefined enumeration is used to characterize the correspondence between the node names of the first infrastructure elements and the character codes of the XML node data.
[0047] It should be noted that there may be a one-to-one correspondence between the node name of the first basic structure element in the predefined enumeration and the character encoding of the XML node data.
[0048] Step 204: convert each XML node data into the first XML format data according to the hierarchy of the first JSON format data.
[0049] Among them, all first basic structure elements in the first JSON format data have their own hierarchy, which can represent the belonging relationship between all first basic structure elements. For example, if the hierarchy of the first basic structure element A is t and the hierarchy of the first basic structure element B is t+1, then it can be considered that the first basic structure element B belongs to the first basic structure element A.
[0050] As an example, step 204 includes: determining the attribution relationship between each XML node data according to the hierarchy of the first JSON format data; and recursively arranging each XML node data into a nested structure according to the attribution relationship between each XML node data to obtain the first XML format data.
[0051] As an example, step 204 includes: determining the attribution relationship between each XML node data according to the hierarchy of the first JSON format data; saving each XML node data as a tree structure according to the attribution relationship between each XML node data, and obtaining the first XML format data, wherein the parent-child relationship between the parent node and the child node in the tree structure corresponds to the attribution relationship between the XML node data.
[0052] In the above data format conversion method, since the predefined enumeration can represent the correspondence between the first infrastructure element of the JOSN format data and the character encoding of the XML node data, the first infrastructure element in the first JSON format data can be quickly converted into the XML node data according to the predefined enumeration, and then according to the hierarchy of the first JSON format data, the XML node data corresponding to each first infrastructure element can be converted into the final first XML format data, so that the first JSON format data can be automatically converted into the first XML format data. Based on this, the data format incompatibility problem between the software module using the JSON format data and the service using the XML format data can be overcome, and the data format incompatibility problem between the software module using the XML format data and the service using the JSON format data can be overcome. Therefore, the service and software module with the incompatibility problem between the JSON format data and the XML format data can also interact directly, which can improve the convenience of using the software module.
[0053] In one embodiment, as shown in FIG. 2 , converting the first basic structure element in the first JSON format data into XML node data according to a predefined enumeration includes:
[0054] Step 302: Detect the type of the node value of the first basic structure element.
[0055] Among them, in this embodiment, a first format conversion function is provided, and the first format conversion function is used to convert a JSON object (JSON format data) into a string data in XML format (XML format data), for example, the JSON object can be converted into XML string data that complies with the BPMN 2.0 specification, etc.; an auxiliary function is provided in the first format conversion function, and the auxiliary function is used to convert the first basic structure element into a corresponding XML string (XML node data), for example, the node name and node value of the first basic structure element are input into the auxiliary function, and the auxiliary function can convert the node name and node value into corresponding character codes, and output these character codes as XML strings.
[0056] As an example, the first format conversion function may be named JsonToxml.
[0057] As an example, a node value can be a string, a Boolean value, a number, an object, or an array.
[0058] Step 304: If the type of the node value is an object type, construct an XML node corresponding to the first basic structure element according to a predefined enumeration to obtain XML node data.
[0059] As an example, step 304 includes: if the type of the node value is an object type, using a predefined enumeration, converting the node name of the first basic structure element into a start tag; using a predefined enumeration, converting the node sub-attributes in the node value into corresponding character codes; adding the character codes corresponding to all node sub-attributes to the start tag; using a predefined enumeration, converting the node name of the first basic structure element into an end tag, at which time the XML node is constructed, and the data consisting of the start tag and the end tag of the XML node is used as XML node data.
[0060] In one embodiment, constructing an XML node corresponding to the first infrastructure element according to a predefined enumeration includes:
[0061] According to the predefined enumeration and the node name of the first basic structure element, the start tag of the XML node is constructed; the node attribute of the first basic structure element is added to the start tag and the preset attribute operation object; according to the predefined enumeration and the node name of the first basic structure element, the end tag of the XML node is constructed.
[0062] Specifically, the node name of the first basic structure element is extracted; the character encoding corresponding to the node name is searched in a predefined enumeration, and the start tag of the XML node is constructed according to the found character encoding; the attribute list of the first basic structure element is traversed to obtain multiple node attributes of the first basic structure element; according to the types of the multiple node attributes, these node attributes are respectively added to the start tag or the preset attribute operation object; according to the character encoding corresponding to the node name, the end tag of the XML node is constructed.
[0063] As an example, the preset attribute operation object may be an attributes object, and node attributes that are not related to the start tag and cannot be found in the predefined enumeration may be stored in the preset attribute operation object.
[0064] Step 306: If the type of the node value is a first preset type, convert the first basic structure element into corresponding XML node data according to a predefined enumeration, wherein the node value of the first preset type is a string, a Boolean value, or a number.
[0065] Among them, when the auxiliary function detects that the node value is a string, Boolean value or number, it will directly return the node value.
[0066] As an example, step 306 includes: if the type of the node value is a first preset type, converting the node name of the first basic structure element into a corresponding character code, and using the converted character code as the node name of the XML node; using the node value of the first basic structure element directly as the node value of the XML node, and then the XML node can be obtained; using the data consisting of the XML node name and node value as XML node data, wherein the node value of the first preset type is a string, a Boolean value or a number.
[0067] Step 308: If the type of the node value is the second preset type, the first basic structure element is converted into an empty node to obtain XML node data, wherein the node value of the first preset type is a meaningless value or an undefined value.
[0068] Among them, the meaningless value can be null, and the undefined value refers to a pre-defined value, the computer does not know the meaning of the undefined value, and the undefined value can be undefined.
[0069] As an example, step 308 includes: if the type of the node value is a second preset type, converting the node name of the first basic structure element into a corresponding character code, and using the converted character code as the node name of the XML node; setting the node value of the XML node to a null value; and using the data consisting of the XML node name and node value as XML node data.
[0070] In this embodiment, the type of the node value of the first basic structure element is first detected, and according to the type of the node value type, the auxiliary function is called to adopt different strategies to accurately convert the first basic structure element into the corresponding XML node data. In this way, the first basic structure element can be quickly and accurately converted into XML node data according to the characteristics of the node value in the first basic structure element, which helps to improve the accuracy and efficiency of generating XML node data.
[0071] In one embodiment, according to the predefined enumeration and the start tag, adding the node attribute of the first infrastructure element to the start tag and the preset attribute operation object includes:
[0072] Determine defined attributes and undefined attributes except the start tag in the node attributes of the first infrastructure element; convert the defined attributes into character codes according to a predefined enumeration and add them to the start tag; add the undefined attributes corresponding to the start tag to the start tag, and add the undefined attributes not corresponding to the start tag to the preset attribute operation object.
[0073] Among them, defined attributes are node attributes declared in the predefined enumeration, and can also be called public attributes of JSON format data; undefined attributes are node attributes not declared in the predefined enumeration, and can also be called private attributes of JSON format data.
[0074] Specifically, all node attributes in the first infrastructure element except the start tag are divided into defined attributes and undefined attributes; the character codes corresponding to the defined attributes are searched in the predefined enumeration, and the character codes corresponding to the defined attributes are added to the start tag; the undefined attributes corresponding to the start tag are added to the start tag, and the undefined attributes that do not correspond to the start tag are added to the preset attribute operation object.
[0075] As an example, the second undefined attribute may be a business attribute.
[0076] In one embodiment, the data format conversion method further includes:
[0077] If there are child nodes in the node attributes of the first infrastructure element, a child node tag corresponding to the child node is constructed according to a predefined enumeration, and the child node tag is added to the start tag; according to the predefined enumeration, the child attributes of the child node are added to the child node tag and the preset attribute operation object.
[0078] Specifically, if there is a child node in the node attribute of the first basic structure element, the child node name of the child node is obtained; the character code corresponding to the child node name is searched in the predefined enumeration, and the child node tag corresponding to the child node is constructed according to the character code corresponding to the child node name; the child node tag is added to the start tag; the defined attributes and undefined attributes outside the child node tag are divided in the child attributes of the child node; the character code corresponding to the defined attribute of the child node is searched in the predefined enumeration, and the character code corresponding to the defined attribute of the child node is added to the start tag; the undefined attribute corresponding to the child node tag is added to the start tag; the undefined attribute that does not correspond to the child node tag is added to the preset attribute operation object. In this way, the child nodes in the first basic structure element can also be recursively processed as the most basic node attribute values, and saved in the constructed start tag and preset attribute operation object, which helps to accurately convert the first basic structure element into XML node data.
[0079] In addition, it should be noted that if there are child nodes in the node attributes, the auxiliary function will be repeatedly called to implement the processing of the child nodes in the above embodiment until all the child nodes in the node attributes are processed.
[0080] In the above embodiment, the node attributes of the first infrastructure element are first divided into defined attributes and undefined attributes. For defined attributes, they are directly added to the start tag. For undefined attributes, the undefined attributes corresponding to the start tag are added to the start tag, and the undefined attributes unrelated to the start tag are added to the preset attribute operation object. In this way, the node attributes of JSON format data are classified and saved into different collections, which is convenient for searching node attributes in the XML format data generated later.
[0081] In one embodiment, the data format conversion method further includes:
[0082] If the type of the node value is an array type, all array elements in the node value are traversed; each array element is converted into XML node data according to a predefined enumeration; the XML node data of each array element is concatenated to obtain the XML node data corresponding to the first basic structure element.
[0083] If the node value is an array, each array element in the array can be considered as a first basic structure element.
[0084] Specifically, if the type of the node value is an array type, all array elements in the node value are traversed; for each array element, the array element is used as the basic structure element of the JSON format data, and the basic structure element is converted into the corresponding XML node data according to a predefined enumeration; the XML node data corresponding to all array elements are concatenated to obtain the XML node data corresponding to the first basic structure element in the first JSON format data.
[0085] As an example, the specific implementation process of converting the basic structure elements into corresponding XML node data according to the predefined enumeration can refer to the contents of the above steps 302 to 308, which will not be repeated here.
[0086] In the above embodiment, when the type of the node value of the first basic structure element is an array type, all array elements in the node value are automatically traversed, and each array element is used as a basic structure element, so that all the basic structure elements are converted into corresponding XML node data according to the predefined enumeration; finally, the XML node data corresponding to all the basic structure elements are spliced together to obtain the XML node data corresponding to the first basic structure element in the first JSON format data. In this way, even if the node value of the first basic structure element is an array type, the first basic structure element can be accurately converted into XML node data, which helps to improve the accuracy of converting the first JSON format data into XML format data.
[0087] Referring to FIG. 3 , FIG. 3 is a schematic diagram of a visualization process of converting JOSN format data into XML format data in one embodiment, as follows:
[0088] First, obtain the first JSON format data output by the graph editing engine, and detect the type of the node value of the first basic structure element in the first JSON format data; if the node value of the first basic structure element is an object, extract the node name of the first basic structure element, and find the character encoding corresponding to the node name in a predefined enumeration; construct the start tag of the XML node according to the character encoding corresponding to the node name, traverse the attribute list of the first basic structure element, and obtain multiple node attributes of the first basic structure element; divide all node attributes of the first basic structure element except the start tag into defined attributes and undefined attributes; find the character encoding corresponding to the defined attributes in the predefined enumeration, and add the character encoding corresponding to the defined attributes to the start tag; add the undefined attributes corresponding to the start tag to the start tag, and add the undefined attributes that do not correspond to the start tag to the preset attribute operation object; construct the end tag of the XML node according to the character encoding corresponding to the node name, and finally use the start tag, the preset attribute operation object and the end tag as XML node data.
[0089] Furthermore, if the first basic structure element is a string, a Boolean value or a number, the node name of the first basic structure element is converted into a corresponding character code according to a predefined enumeration, and the converted character code is used as the node name of the XML node; the node value of the first basic structure element is directly used as the node value of the XML node. At this time, the data consisting of the XML node name and node value can be output as XML node data.
[0090] Furthermore, if the first basic structure element is a meaningless value or an undefined value, the node name of the first basic structure element is converted into a corresponding character code according to a predefined enumeration, and the converted character code is used as the node name of the XML node; the node value of the XML node is set to a null value; and the data consisting of the XML node name and node value is output as XML node data.
[0091] Furthermore, according to the hierarchy of the first JSON format data, the attribution relationship between each XML node data is determined; according to the attribution relationship between each XML node data, each XML node data is recursively formed into a nested structure to obtain the first XML format data, thereby completing the data format conversion process from JSON format data to XML format data.
[0092] The above-mentioned embodiment can realize the automatic conversion of the first JSON format data into the first XML format data. Based on this, it can overcome the data format incompatibility problem between the graph editing engine using JSON format data and the service using XML format data. Therefore, the service and the graph editing engine with the incompatibility problem between JSON format data and XML format data can also interact directly, which can improve the ease of use of the graph editing engine.
[0093] In one embodiment, referring to FIG. 4 , the data format conversion method further includes:
[0094] Step 402: Acquire second XML format data, and convert the second XML format data into a root node element object.
[0095] Among them, the second XML format data can be XML format data output by a software module that is only compatible with XML format data. In order to enable external services that use JSON format data to interact with the software module, the present application needs to convert the second XML format data into JSON format data; the second XML format data can also be XML format data input into the software module for external service requests that are only compatible with XML format data. In order to enable the software module that uses JSON format data to directly interact with external services that are only compatible with XML format data, the present application needs to convert the second XML format data into JSON format data.
[0096] As an example, a DOMParser instance may be used to parse the second XML format data into a DOM object, and the DOM object is a root node element object.
[0097] As an example, ActiveXObject may also be used to parse the second XML format data to obtain the root node element object.
[0098] Step 404: Generate general JSON format data corresponding to the second XML format data according to the node attributes and node type of the root node element object.
[0099] In this embodiment, a second format conversion function is also provided, and the second format conversion function is used to convert the XML object (XML format data) into JSON format data.
[0100] As an example, the second format conversion function may be named xmlToJson.
[0101] As an example, step 404 includes: calling the second format conversion function to traverse the element nodes in the root node element object, and determining the node attributes and node type of the element nodes; if the node type is an instruction node, a text node, or a comment node, then according to a predefined enumeration, converting the node name and node value of the element node into a second infrastructure element; if the node type is not an instruction node, a text node, or a comment node, then according to a predefined enumeration, converting all node attributes in the attribute list of the element node into corresponding second infrastructure elements; and combining the data consisting of all second infrastructure elements into general JSON format data.
[0102] As an example, the second infrastructure element may be a key-value pair, and each second infrastructure element may be considered as a node in JSON format data. The above-mentioned conversion of the node name and node value of the element node into the second infrastructure element includes:
[0103] Using the character code corresponding to the node name of the element node in the predefined enumeration as the key name and the node value of the element node as the key value, the node name and node value of the element node are converted into a key-value pair, and the converted key-value pair is used as the second basic structure element.
[0104] As an example, according to a predefined enumeration, all node attributes in the attribute list of the element node are converted into corresponding second infrastructure elements, including:
[0105] For each node attribute in the attribute list of the element node, obtain the attribute name and attribute value in the node attribute; use the character code corresponding to the attribute name in the predefined enumeration as the key name, and the attribute value as the key value, convert the meta-attribute name and attribute value into a key-value pair, and use the converted key-value pair as the second basic structure element.
[0106] In one embodiment, the root node element object includes at least one element node, and the general JSON format data includes a second basic structure element corresponding to the element node; according to the node attribute and node type of the root node element object, the general JSON format data corresponding to the second XML format data is generated, including:
[0107] Detect the node type of the element node; if the node type is an instruction node, a text node or a comment node, generate a second basic structure element corresponding to the element node according to a predefined enumeration and the node value of the element node; if the node type is not an instruction node, a text node or a comment node, traverse the attribute list of the element node to obtain N attribute names and N attribute values of the element node, where N is a positive integer; generate N second basic structure elements corresponding to the element node according to the N attribute names and the N attribute values.
[0108] Among them, the root node element object includes one or more element nodes, which are XML nodes. Accordingly, the general JSON format data includes a second basic structure element corresponding to the element node, and the second basic structure element is a key-value pair; N is the number of attributes in the attribute list of the element node, each attribute consists of an attribute name and an attribute value, and N is a positive integer.
[0109] Specifically, the node type of the element node is detected. If the node type is an instruction node, a text node or a comment node, the node name of the element node is converted into a corresponding character code according to a predefined enumeration, and a first key-value pair is generated using the character code corresponding to the node name as a key name and the node value of the element node as a key value, and the first key-value pair is used as the second basic structure element; if the node type is not an instruction node, a text node or a comment node, the attribute list of the element node is traversed to obtain N attribute names of the element node and N attribute values corresponding to the N attribute names; for each attribute name, the attribute name is converted into a corresponding character code according to a predefined enumeration, and a second key-value pair is generated using the character code corresponding to the attribute name as a key name and the attribute value corresponding to the attribute name as a key value, and the second key-value pairs corresponding to the N attribute names are used as N second basic structure elements.
[0110] Step 406: Convert the general JSON format data into second JSON format data according to the predefined enumeration.
[0111] Among them, the predefined enumeration is also used to declare the correspondence between characters in the general JSON format data and characters in the second JOSN format data. The second JSON format data can be JSON format data suitable for the target software module. The second JSON format data and the general JSON format data used by different software modules usually have differences in character formats. For example, the target software module can be a graph editing engine, and the second JSON format data and the general JSON format data used by the graph editing engine will have differences in character formats.
[0112] As an example, step 406 includes: performing character format conversion on the general JSON format data according to a predefined enumeration to obtain second JSON format data.
[0113] Among them, the second basic structure element is the basic unit that constitutes the general JSON format data, which can be specifically a key-value pair in the general JSON format data. Each key-value pair can be considered as a node in the general JSON format data. The key name of the key-value pair is the node name, and the key value of the key-value pair is the node value.
[0114] In one embodiment, generating N second basic structure elements corresponding to the element node according to the N attribute names and the N attribute values includes:
[0115] Create an empty object and a counter object, and store the attribute name and attribute value in the form of a key-value pair in the empty object to obtain a target object; if the attribute name is the same as the key name of the existing JSON data, then according to the counter object, add the corresponding counter after the key name corresponding to the attribute name in the target object to obtain a second basic structure element, wherein the counter is used to identify the key name; if the attribute name is not the same as the key name of the existing JSON data, the target object is determined as the second basic structure element.
[0116] The existing JSON format data may be the JSON format data originally existing in the software module, or may be the JSON format data previously generated during the data format conversion this time.
[0117] Specifically, for each attribute name and the attribute value corresponding to each attribute name, an empty object and a counter object are created; according to the predefined enumeration, the attribute name is converted into the corresponding character code, and the character code corresponding to the attribute name is used as the key name and the attribute value corresponding to the attribute name is used as the key value, and the character code corresponding to the attribute name and the attribute value are stored in the empty object in the form of a key-value pair to obtain a target object; if the attribute name is the same as the key name in the existing JSON format data, the counter object is called, and the corresponding counter is added after the key name corresponding to the attribute name in the target object to obtain a second basic structure element, wherein the counter is used to identify the key name; if the attribute name is different from the key name in the existing JSON format data, the target object is output as the second basic structure element. In this way, in this embodiment, when the newly added key name in the existing JSON format data is the same as the existing key name, a counter can be added after the newly added key name to distinguish the newly added key name from the previously existing key name, thereby ensuring the uniqueness of the key name and helping to improve the accuracy of the second JSON format data finally generated.
[0118] In the above embodiment, the second XML format data is first obtained, and the second XML format data is first converted into a root node element object, and then the general JSON format data corresponding to the second XML format data is generated according to the node attributes and node types of the root node element object; finally, according to the predefined enumeration, the general JSON format data is converted into the second JSON format data compatible with the software module. In this way, the second XML format data is automatically converted into the second JSON format data that can be received by the software module. Based on this, the data format incompatibility problem between the software module using JSON format data and the service using XML format data can be overcome. Therefore, the service and software module with the incompatibility problem between JSON format data and XML format data can also interact directly, which can improve the convenience of using the software module.
[0119] Referring to FIG. 5 , FIG. 5 is a schematic diagram of a visualization process of converting XML format data into JSON format data in one embodiment, as follows:
[0120] First, obtain the second XML format data of the external service request input graph editing engine, and convert the second XML format data into a root node object; traverse the element nodes in the root node element object, and detect the node type of the element node; if the node type is an instruction node, text node or comment node, then according to a predefined enumeration, convert the node name of the original node into a corresponding character encoding, using the character encoding as the key name and the node value of the element node as the key value, convert the node name and node value of the element node into a key-value pair, and use the converted key-value pair as the second basic structure element.
[0121] Furthermore, if the node type is not an instruction node, a text node or a comment node, then for the N node attributes in the attribute list of the element node, for each node attribute, obtain the attribute name and attribute value in the node attribute; using the character encoding corresponding to the attribute name in the predefined enumeration as the key name, and the attribute value as the key value, convert the meta-attribute name and attribute value into a key-value pair, and use the converted key-value pair as the second basic structure element, thereby obtaining N second basic structure elements.
[0122] Further, all the obtained data sets consisting of the second infrastructure elements are combined as general JSON format data; according to a predefined enumeration, the characters in the general JSON format data are converted into character format to obtain second JSON format data that can be received by the graph editing engine.
[0123] The above-mentioned embodiment can realize the automatic conversion of the second XML format data into the second JSON format data receivable by the graph editing engine. Based on this, it can overcome the data format incompatibility problem between the graph editing engine using JSON format data and the service using XML format data. Therefore, the service and the graph editing engine with the incompatibility problem between JSON format data and XML format data can also interact directly, which can improve the ease of use of the graph editing engine.
[0124] It should be understood that, although the various steps in the flowcharts involved in the above-mentioned embodiments are displayed in sequence according to the indication of the arrows, these steps are not necessarily executed in sequence according to the order indicated by the arrows. Unless there is a clear explanation in this article, the execution of these steps does not have a strict order restriction, and these steps can be executed in other orders. Moreover, at least a part of the steps in the flowcharts involved in the above-mentioned embodiments can include multiple steps or multiple stages, and these steps or stages are not necessarily executed at the same time, but can be executed at different times, and the execution order of these steps or stages is not necessarily to be carried out in sequence, but can be executed in turn or alternately with other steps or at least a part of the steps or stages in other steps.
[0125] Based on the same inventive concept, the embodiment of the present application also provides a data format conversion device for implementing the data format conversion method involved above. The implementation scheme for solving the problem provided by the device is similar to the implementation scheme recorded in the above method, so the specific limitations in one or more data format conversion device embodiments provided below can refer to the limitations of the data format conversion method above, and will not be repeated here.
[0126] In one embodiment, as shown in FIG6 , a data format conversion device is provided, including: a first conversion module 502 and a second conversion module 504, wherein:
[0127] The first conversion module is used to convert a first basic structure element in the first JSON format data into XML node data according to a predefined enumeration, wherein the predefined enumeration is used to characterize the correspondence between the node name of the first basic structure element and the character encoding of the XML node data.
[0128] The second conversion module is used to convert each of the XML node data into the first XML format data according to the level of the first JSON format data.
[0129] In one embodiment, the first conversion module is further configured to:
[0130] Detect the type of the node value of the first infrastructure element; if the type of the node value is an object type, construct an XML node of the first infrastructure element according to the predefined enumeration to obtain XML node data; if the type of the node value is a first preset type, convert the first infrastructure element into corresponding XML node data according to the predefined enumeration, wherein the node value of the first preset type is a string, a Boolean value or a number; if the type of the node value is a second preset type, convert the first infrastructure element into an empty node to obtain XML node data, wherein the node value of the first preset type is a meaningless value or an undefined value.
[0131] In one embodiment, the first conversion module is further configured to:
[0132] According to the predefined enumeration and the node name of the first infrastructure element, a start tag of the XML node is constructed; the node attributes of the first infrastructure element are added to the start tag and the preset attribute operation object; according to the predefined enumeration and the node name of the first infrastructure element, an end tag of the XML node is constructed.
[0133] In one embodiment, the first conversion module is further configured to:
[0134] Determine defined attributes and undefined attributes other than the start tag in the node attributes of the first infrastructure element; convert the defined attributes into character codes according to the predefined enumeration and add them to the start tag; add the undefined attributes corresponding to the start tag to the start tag, and add the undefined attributes that do not correspond to the start tag to the preset attribute operation object.
[0135] In one embodiment, the first conversion module is further configured to:
[0136] If there is a child node in the node attribute of the first infrastructure element, then according to the predefined enumeration, a child node tag corresponding to the child node is constructed, and the child node tag is added to the start tag; according to the predefined enumeration, the child attributes of the child node are added to the child node tag and the preset attribute operation object.
[0137] In one embodiment, the first conversion module is further configured to:
[0138] If the type of the node value is an array type, all array elements in the node value are traversed; each array element is converted into XML node data according to a predefined enumeration; the XML node data of each array element is concatenated to obtain the XML node data corresponding to the first basic structure element.
[0139] In one embodiment, the data format conversion device further includes:
[0140] The third conversion module is used to obtain the second XML format data and convert the second XML format data into a root node element object; generate general JSON format data corresponding to the second XML format data according to the node attributes and node types of the root node element object; and convert the general JSON format data into the second JOSN format data according to the predefined enumeration.
[0141] In one embodiment, the root node element object includes at least one element node, and the general JSON format data includes a second basic structure element corresponding to the element node; and the third conversion module is further used to:
[0142] Detect the node type of the element node; if the node type is an instruction node, a text node, or a comment node, generate a second basic structure element corresponding to the element node according to the predefined enumeration and the node value of the element node; if the node type is not an instruction node, a text node, or a comment node, traverse the attribute list of the element node to obtain N attribute names and N attribute values of the element node, where N is a positive integer; generate N second basic structure elements corresponding to the element node according to the N attribute names and the N attribute values. .
[0143] In one embodiment, the third conversion module is further used for:
[0144] Create an empty object and a counter object, and store the attribute name and the attribute value in the form of a key-value pair in the empty object to obtain a target object; if the attribute name is the same as the key name of the existing JSON data, then according to the counter object, add a corresponding counter after the key name corresponding to the attribute name in the target object to obtain a second basic structure element, wherein the counter is used to identify the key name; if the attribute name is not the same as the key name of the existing JSON data, determine the target object as the second basic structure element.
[0145] Each module in the above data format conversion device can be implemented in whole or in part by software, hardware, or a combination thereof. Each module can be embedded in or independent of a processor in a computer device in the form of hardware, or can be stored in a memory in a computer device in the form of software, so that the processor can call and execute operations corresponding to each module.
[0146] In one embodiment, a computer device is provided, which may be a server, and its internal structure diagram may be shown in FIG7 . The computer device includes a processor, a memory, and a network interface connected via a system bus. The processor of the computer device is used to provide computing and control capabilities. The memory of the computer device includes a non-volatile storage medium and an internal memory. The non-volatile storage medium stores an operating system, a computer program, and a database. The internal memory provides an environment for the operation of the operating system and the computer program in the non-volatile storage medium. The database of the computer device is used to store data format conversion data. The network interface of the computer device is used to communicate with an external terminal via a network connection. When the computer program is executed by the processor, a data format conversion method is implemented.
[0147] Those skilled in the art will understand that the structure shown in FIG. 7 is merely a block diagram of a partial structure related to the solution of the present application, and does not constitute a limitation on the computer device to which the solution of the present application is applied. The specific computer device may include more or fewer components than shown in the figure, or combine certain components, or have a different arrangement of components.
[0148] In one embodiment, a computer device is further provided, including a memory and a processor, wherein a computer program is stored in the memory, and the processor implements the steps in the above method embodiments when executing the computer program.
[0149] In one embodiment, a computer-readable storage medium is provided, on which a computer program is stored. When the computer program is executed by a processor, the steps in the above-mentioned method embodiments are implemented.
[0150] In one embodiment, a computer program product is provided, including a computer program, which implements the steps in the above method embodiments when executed by a processor.
[0151] Those skilled in the art can understand that all or part of the processes in the above-mentioned embodiment methods can be completed by instructing the relevant hardware through a computer program, and the computer program can be stored in a non-volatile computer-readable storage medium. When the computer program is executed, it can include the processes of the embodiments of the above-mentioned methods. Among them, any reference to the memory, database or other medium used in the embodiments provided in this application can include at least one of non-volatile and volatile memory. Non-volatile memory can include read-only memory (ROM), magnetic tape, floppy disk, flash memory, optical memory, high-density embedded non-volatile memory, resistive random access memory (ReRAM), magnetoresistive random access memory (MRAM), ferroelectric random access memory (FRAM), phase change memory (PCM), graphene memory, etc. Volatile memory can include random access memory (RAM) or external cache memory, etc. As an illustration and not limitation, RAM can be in various forms, such as static random access memory (SRAM) or dynamic random access memory (DRAM). The database involved in each embodiment provided in this application may include at least one of a relational database and a non-relational database. Non-relational databases may include distributed databases based on blockchains, etc., but are not limited to this. The processor involved in each embodiment provided in this application may be a general-purpose processor, a central processing unit, a graphics processor, a digital signal processor, a programmable logic device, a data processing logic device based on quantum computing, etc., but are not limited to this.
[0152] The technical features of the above embodiments may be combined arbitrarily. To make the description concise, not all possible combinations of the technical features in the above embodiments are described. However, as long as there is no contradiction in the combination of these technical features, they should be considered to be within the scope of this specification.
[0153] The above-described embodiments only express several implementation methods of the present application, and the descriptions thereof are relatively specific and detailed, but they cannot be understood as limiting the scope of the present application. It should be pointed out that, for a person of ordinary skill in the art, several variations and improvements can be made without departing from the concept of the present application, and these all belong to the protection scope of the present application. Therefore, the protection scope of the present application shall be subject to the attached claims.
Claims
1. A data format conversion method, characterized in that: The method comprises: According to a predefined enumeration, converting a first basic structure element in the first JSON format data into XML node data, wherein the predefined enumeration is used to characterize the correspondence between the node name of the first basic structure element and the character encoding of the XML node data; According to the level of the first JSON format data, each of the XML node data is converted into the first XML format data.
2. The method according to claim 1, characterized in that The converting the first basic structure element in the first JSON format data into XML node data according to the predefined enumeration includes: Detecting the type of the node value of the first infrastructure element; If the type of the node value is an object type, constructing an XML node of the first infrastructure element according to the predefined enumeration to obtain XML node data; If the type of the node value is a first preset type, converting the first infrastructure element into corresponding XML node data according to the predefined enumeration, wherein the node value of the first preset type is a string, a Boolean value, or a number; If the type of the node value is a second preset type, the first basic structure element is converted into an empty node to obtain XML node data, wherein the node value of the first preset type is a meaningless value or an undefined value.
3. The method according to claim 2, characterized in that The step of constructing an XML node corresponding to the first infrastructure element according to a predefined enumeration includes: Constructing a start tag of the XML node according to the predefined enumeration and the node name of the first infrastructure element; Adding the node attribute of the first infrastructure element to the start tag and the preset attribute operation object; An end tag of the XML node is constructed according to the predefined enumeration and the node name of the first infrastructure element.
4. The method according to claim 3, characterized in that The adding the node attribute of the first infrastructure element to the start tag and the preset attribute operation object according to the predefined enumeration and the start tag includes: Determining defined attributes and undefined attributes other than the start tag in the node attributes of the first infrastructure element; According to the predefined enumeration, convert the defined attributes into character codes and add them into the start tag; The undefined attributes corresponding to the start tag are added to the start tag, and the undefined attributes not corresponding to the start tag are added to the preset attribute operation object.
5. The method according to claim 3, characterized in that: The method further comprises: If there is a child node in the node attribute of the first infrastructure element, construct a child node tag corresponding to the child node according to the predefined enumeration, and add the child node tag to the start tag; According to the predefined enumeration, the sub-attributes of the sub-node are added to the sub-node tag and the preset attribute operation object.
6. The method according to claim 2, characterized in that The method further comprises: If the type of the node value is an array type, then traverse all array elements in the node value; According to a predefined enumeration, convert each of the array elements into XML node data; The XML node data of each of the array elements are concatenated to obtain the XML node data corresponding to the first basic structure element.
7. The method according to claim 1, characterized in that The method further comprises: Acquire second XML format data, and convert the second XML format data into a root node element object; Generate general JSON format data corresponding to the second XML format data according to the node attributes and node type of the root node element object; According to the predefined enumeration, the general JSON format data is converted into second JSON format data.
8. The method according to claim 7, characterized in that The root node element object includes at least one element node, and the general JSON format data includes a second basic structure element corresponding to the element node; The generating, according to the node attribute and the node type of the root node element object, the general JSON format data corresponding to the second XML format data comprises: Detecting the node type of the element node; If the node type is an instruction node, a text node or a comment node, generating a second basic structure element corresponding to the element node according to the predefined enumeration and the node value of the element node; If the node type is not an instruction node, a text node, or a comment node, traverse the attribute list of the element node to obtain N attribute names and N attribute values of the element node, where N is a positive integer; According to the N attribute names and the N attribute values, N second basic structure elements corresponding to the element node are generated.
9. The method according to claim 8, characterized in that The generating, according to the N attribute names and the N attribute values, N second basic structure elements corresponding to the element node comprises: Create an empty object and a counter object, and store the attribute name and the attribute value in the form of a key-value pair in the empty object to obtain a target object; If the attribute name is the same as the key name of the existing JSON data, then according to the counter object, a corresponding counter is added after the key name corresponding to the attribute name in the target object to obtain a second basic structure element, wherein the counter is used to identify the key name; If the attribute name is different from the key name of the existing JSON data, the target object is determined as the second basic structure element.
10. A data format conversion device, characterized in that: The device comprises: A first conversion module, configured to convert a first basic structure element in the first JSON format data into XML node data according to a predefined enumeration, wherein the predefined enumeration is used to characterize a correspondence between a node name of the first basic structure element and a character encoding of the XML node data; The second conversion module is used to convert each of the XML node data into the first XML format data according to the level of the first JSON format data.
11. A computer device comprising a memory and a processor, wherein the memory stores a computer program, wherein: When the processor executes the computer program, the steps of the method according to any one of claims 1 to 9 are implemented.
12. A computer-readable storage medium having a computer program stored thereon, characterized in that: When the computer program is executed by a processor, the steps of the method according to any one of claims 1 to 9 are implemented.
13. A computer program product, comprising a computer program, characterized in that When the computer program is executed by a processor, the steps of the method according to any one of claims 1 to 9 are implemented.
