A data conversion method and apparatus

By using custom Map structure data and attribute annotation information, combined with reflection mechanism, we have achieved efficient conversion between XML data and Java objects, solved the scalability problem when XML data format is not fixed, and improved the processing efficiency of transaction process.

CN114168149BActive Publication Date: 2026-01-23WEBANK (CHINA)
View PDF 2 Cites 0 Cited by

Patent Information

Application Number
CN202111499245.X
Authority / Receiving Office
CN · China
Patent Type
Patents(China)
Current Assignee / Owner
Filing Date
2021-12-09
Publication Date
2026-01-23
Estimated Expiration
2041-12-09

AI Technical Summary

Technical Problem

Existing technologies have low efficiency in converting XML data to Java objects, especially when the XML data format is not fixed. The mapping and parsing methods have poor scalability, which affects the efficiency of transaction processing.

Method used

By using custom Map structure data and attribute annotation information, combined with reflection mechanism, the conversion of XML data to Java objects is completed automatically, and the reverse conversion of Java objects to XML data is achieved through custom path mapping rules.

Benefits of technology

It improves the efficiency of converting XML data to Java objects, supports rapid conversion between different XML data formats, and enhances the processing efficiency of transaction processes.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN114168149B_ABST
    Figure CN114168149B_ABST
Patent Text Reader

Abstract

The application discloses a data conversion method and device, the method comprises the following steps: reading XML data to be converted to obtain a first file object; parsing the first file object to obtain first custom Map structure data; the first custom Map structure data comprises a key value determined based on a node path and a node characteristic and a value value corresponding to the key value; attribute annotation information of a custom Java object corresponding to the first file object is determined, a mapping key value corresponding to the custom Java object is determined based on the attribute annotation information; a matching value value matched with the mapping key value in the first custom Map structure data is found, the matching value value is reflected and assigned to the custom JAVA object to obtain a target Java object. When the method is applied to Fintech, the conversion efficiency of mutual conversion between XML data and Java objects is improved.
Need to check novelty before this filing date? Find Prior Art

Description

TECHNICAL FIELD

[0001] Embodiments of the present application relate to the field of financial technology (Fintech), and in particular to a data conversion method and device. BACKGROUND

[0002] With the development of computer technology, more and more technologies are applied in the financial field, and the traditional financial industry is gradually changing to financial technology. However, due to the security and real-time requirements of the financial industry, higher requirements are also put forward for the technology.

[0003] At present, in the financial industry, the entire transaction process of electronic commercial bills is realized by the message communication between each member institution and the stock exchange. It can be seen that message analysis is the basis of message data processing in the entire transaction process. Since the message body in the message is in XML format, and there are many types of messages and deep levels, how to parse it into a Java object used by the system, and how to convert the processed Java object into XML format data, has become a problem to be solved.

[0004] In view of this, the related technology provides a scheme of using a Java parsing component to parse XML data into related data objects, and then converting the data objects into custom Java objects. However, when the XML data format is not fixed, each type of XML data needs to determine the parsing mapping relationship again. It can be seen that the mapping and parsing method provided in the related technology has poor expansibility, which leads to low conversion efficiency of XML data and Java objects, and further affects the transaction process processing efficiency. SUMMARY

[0005] The present application provides a data conversion method and device to solve the problem of low conversion efficiency of XML data and Java objects in the prior art.

[0006] In a first aspect, the present application provides a data conversion method, comprising: reading XML data to be converted to obtain a first file object; parsing the first file object to obtain a first custom Map structure data; the first custom Map structure data includes a key value determined based on a node path and a node characteristic, and a value corresponding to the key value; determining attribute annotation information of a custom Java object corresponding to the first file object, and based on the attribute annotation information, determining a mapping key value corresponding to the custom Java object; finding a matching value in the first custom Map structure data that matches the mapping key value, and reflecting the assignment of the matching value to the custom JAVA object to obtain a target Java object corresponding to the XML data to be converted.

[0007] In the method, the XML data to Java object conversion is automatically completed through the first custom Map structure data and attribute annotation information, reflection, etc., and the data conversion efficiency is improved.

[0008] Optionally, after obtaining the target Java object corresponding to the XML data to be converted, the method further includes: obtaining a new Java object after operation on the target Java object; obtaining annotation attribute information corresponding to the new Java object through reflection processing on the new Java object, and storing path information and data in the annotation attribute information into second custom Map structure data; creating a second file object, and establishing an object tree corresponding to the second file object based on the path information and data in the second custom Map structure data; and performing reverse analysis processing on the object tree through a preset plug-in to generate target XML data corresponding to the new Java object.

[0009] In the method, the Java object to XML data conversion is realized based on the second custom Map structure data and the preset plug-in, and the data conversion efficiency is improved.

[0010] Optionally, the establishment of the object tree corresponding to the second file object based on the path information and data in the second custom Map structure data includes: determining a root node corresponding to the path information in the second custom Map structure data; analyzing each path information in the second custom Map structure data based on a preset analysis rule to respectively obtain a corresponding parent path and a child path, taking a node corresponding to the child path as a child node, and determining a value corresponding to the child node; when information corresponding to the parent path is not a distinguisher, determining whether the root node includes a node corresponding to the parent path; when it is determined that the root node includes the node corresponding to the parent path, taking the node corresponding to the parent path as a child node, and determining a value corresponding to the child node; when it is determined that the root node does not include the node corresponding to the parent path, taking the node corresponding to the parent path as a parent node, and determining a value corresponding to the parent node; when information corresponding to the parent path is a distinguisher, determining that the node corresponding to the parent path is an attribute node, and a data value corresponding to the parent path is a corresponding value; and based on the parent node, the child node, and the attribute node and values corresponding thereto, constructing the object tree corresponding to the second file object.

[0011] Based on the method, the Element object tree can be reversely constructed based on the custom rule in the aforementioned XML data conversion to Java object, i.e., the relationship between the path information and the node, and the conversion between the new Java object and the target XML data is quickly realized.

[0012] Optionally, the first file object is parsed to obtain first custom Map structure data, comprising: parsing the first file object to obtain a root node of the first file object and a child node of the root node; traversing the child node of the root node to determine a key value corresponding to each child node and a value value corresponding to the key value to determine the first custom Map structure data.

[0013] Based on the above method, the first custom Map structure data is provided in the embodiment of the application, and based on the Map structure data, a passing conversion mode can be provided for XML data of different formats, that is, a Java object only needs to inherit the Map structure data to realize mutual conversion of the Java object and the XML, without other parsing class components, so that the data conversion efficiency is improved.

[0014] Optionally, the child node of the root node is traversed to determine a key value corresponding to each child node and a value value corresponding to the key value to determine the first custom Map structure data, comprising: when it is determined that any of the child nodes is an attribute node, any of the child nodes is added to the first custom Map structure data, and it is determined that a key value corresponding to any of the child nodes comprises a root path, a node name, a distinguisher and an attribute name, and a value value corresponding to the key value is an attribute value; when it is determined that any of the child nodes is a leaf node and any of the child nodes does not exist in the first custom Map structure data, any of the child nodes is added to the first custom Map structure data, and it is determined that a key value corresponding to any of the child nodes comprises a root path and a node name, and a value value corresponding to the key value is a node data value; when it is determined that any of the child nodes is a leaf node and any of the child nodes exists in the first custom Map structure data, any of the child nodes is added to the first custom Map structure data, and it is determined that a key value corresponding to any of the child nodes comprises a root path, a node name and a subscript distinguisher, and a value value corresponding to the key value is a node data value.

[0015] Based on the above method, the scheme for recursively realizing conversion of the first file object into Map structure data is provided, and based on the scheme, the efficiency of converting XML data of different formats into an intermediate variable, that is, Map structure data, can be improved.

[0016] Optionally, based on the attribute annotation information, the mapping key value corresponding to the custom Java object is determined, including: when it is determined that the attribute annotation information does not exist same name node information, and the attribute annotation information does not exist XML attribute information, it is determined that the mapping key value includes a path; when it is determined that the attribute annotation information exists same name node information, and the attribute annotation information does not exist XML attribute information, it is determined that the mapping key value includes a path and a same name node serial number in a preset format; when it is determined that the attribute annotation information does not exist same name node information, and the attribute annotation information exists XML attribute information, it is determined that the mapping key value includes a path, a distinguisher and an attribute name; when it is determined that the attribute annotation information exists same name node information, and the attribute annotation information exists XML attribute information, it is determined that the mapping key value includes a path, a distinguisher, an attribute name and a subscript distinguisher.

[0017] Based on the above method, it can be marked by attribute annotation information that the attribute of the Java object is mapped to which position of the XML data, that is, it is marked by the path in the annotation, and the custom path mapping is realized. That is, through the custom designed path mapping rule, the attribute of the Java object is supported to be mapped into the attribute of the node in the XML data.

[0018] Optionally, the attribute annotation information corresponding to the custom Java object is updated based on the XML data to be converted.

[0019] Based on the above method, different annotations corresponding to different formats of XML data can be realized, so that the mutual conversion of the XML data and the JAVA object is realized more quickly, and the data conversion efficiency is improved.

[0020] In a second aspect, the present application provides a data conversion device, including: a first obtaining unit, configured to read the XML data to be converted, and obtain a first file object; a first processing unit, configured to parse the first file object, and obtain a first custom Map structure data; the first custom Map structure data includes a key value determined based on a node path and a node characteristic, and a value value corresponding to the key value; a second processing unit, configured to determine attribute annotation information of a custom Java object corresponding to the first file object, and determine a mapping key value corresponding to the custom Java object based on the attribute annotation information; and a second obtaining unit, configured to find a matching value value in the first custom Map structure data matching the mapping key value, and reflect and assign the matching value value to the custom JAVA object, and obtain a target Java object corresponding to the XML data to be converted.

[0021] Optionally, the apparatus further comprises a third processing unit configured to: obtain a new Java object after operation of the target Java object; obtain annotation attribute information corresponding to the new Java object by reflection processing on the new Java object, and store path information and data in the annotation attribute information into a second custom Map structure data; create a second file object, and complete establishment of an object tree corresponding to the second file object based on the path information and data in the second custom Map structure data; and generate target XML data corresponding to the new Java object by reverse analysis processing on the object tree through a preset plug-in.

[0022] Optionally, the third processing unit is specifically configured to: complete establishment of an object tree corresponding to the second file object based on the path information and data in the second custom Map structure data, including: determining a root node corresponding to the path information in the second custom Map structure data; analyzing each path information in the second custom Map structure data based on a preset analysis rule, respectively obtaining a corresponding parent path and a child path, taking a node corresponding to the child path as a child node, and determining a value corresponding to the child node; when information corresponding to the parent path is not a distinguisher, determining whether the root node includes a node corresponding to the parent path; when it is determined that the root node includes the node corresponding to the parent path, taking the node corresponding to the parent path as a child node, and determining a value corresponding to the child node; when it is determined that the root node does not include the node corresponding to the parent path, taking the node corresponding to the parent path as a parent node, and determining a value corresponding to the parent node; when information corresponding to the parent path is a distinguisher, determining that the node corresponding to the parent path is an attribute node, and a data value corresponding to the parent path is a corresponding value; and constructing the object tree corresponding to the second file object based on the parent node, the child node, and the attribute node, and values corresponding to the parent node, the child node, and the attribute node.

[0023] Optionally, the first processing unit is specifically configured to: analyze the first file object to obtain a root node of the first file object and a child node of the root node; and traverse the child node of the root node to determine a key value corresponding to each child node and a value corresponding to the key value, so as to determine a first custom Map structure data.

[0024] Optionally, the first processing unit is specifically configured to: when determining that any of the sub-nodes is an attribute node, adding any of the sub-nodes to the first custom Map structure data, and determining that a key value corresponding to any of the sub-nodes comprises a root path, a node name, a distinguisher and an attribute name, and a value corresponding to the key value is an attribute value; when determining that any of the sub-nodes is a leaf node and any of the sub-nodes does not exist in the first custom Map structure data, adding any of the sub-nodes to the first custom Map structure data, and determining that a key value corresponding to any of the sub-nodes comprises a root path and a node name, and a value corresponding to the key value is a node data value; and when determining that any of the sub-nodes is a leaf node and any of the sub-nodes exists in the first custom Map structure data, adding any of the sub-nodes to the first custom Map structure data, and determining that a key value corresponding to any of the sub-nodes comprises a root path, a node name and a subscript distinguisher, and a value corresponding to the key value is a node data value.

[0025] Optionally, the second processing unit is configured to: when determining that the attribute annotation information does not exist in the same name node information and the XML attribute information does not exist in the attribute annotation information, determining that a mapping key value comprises a path; when determining that the attribute annotation information exists in the same name node information and the XML attribute information does not exist in the attribute annotation information, determining that a mapping key value comprises a path and a same name node serial number in a preset format; when determining that the attribute annotation information does not exist in the same name node information and the XML attribute information exists in the attribute annotation information, determining that a mapping key value comprises a path, a distinguisher and an attribute name; and when determining that the attribute annotation information exists in the same name node information and the XML attribute information exists in the attribute annotation information, determining that a mapping key value comprises a path, a distinguisher, an attribute name and a subscript distinguisher.

[0026] Optionally, attribute annotation information corresponding to the custom Java object is updated based on the XML data to be converted.

[0027] The beneficial effects of the second aspect and each optional device of the second aspect can refer to the beneficial effects of the first aspect and each optional method of the first aspect, which will not be repeated here.

[0028] In a third aspect, the present application provides a computer device comprising a program or instructions, when the program or instructions are executed, to execute the first aspect and each optional method of the first aspect.

[0029] In a fourth aspect, the present application provides a storage medium comprising a program or instructions which, when executed, perform the method of the first aspect and each of the optional methods of the first aspect. BRIEF DESCRIPTION OF DRAWINGS

[0030] In order to more clearly illustrate the technical solutions in the embodiments of the present application, the drawings needed to be used in the embodiments will be briefly introduced.

[0031] Figure 1 A schematic diagram of a data conversion method in the prior art is provided.

[0032] Figure 2 A schematic diagram of an application scenario provided by the embodiments of the present application is provided.

[0033] Figure 3 A schematic diagram of the steps of a data conversion method provided by the embodiments of the present application is provided.

[0034] Figure 4 A schematic diagram of a path provided by the embodiments of the present application is provided.

[0035] Figure 5 A schematic diagram of attribute annotation information provided by the embodiments of the present application is provided.

[0036] Figure 6 A schematic diagram of conversion of XML data into a Java object provided by the embodiments of the present application is provided.

[0037] Figure 7 A schematic diagram of conversion of a Java object into XML data provided by the embodiments of the present application is provided.

[0038] Figure 8 A structural schematic diagram of a data conversion device provided by the embodiments of the present application is provided. DETAILED DESCRIPTION

[0039] In order to better understand the above technical solutions, the above technical solutions will be described in detail below in combination with the drawings of the specification and the specific embodiments. It should be understood that the specific features of the embodiments of the present application and the embodiments are detailed descriptions of the technical solutions of the present application, and are not limitations of the technical solutions of the present application. In the case of no conflict, the technical features of the embodiments of the present application and the embodiments can be combined with each other.

[0040] It is to be understood that the terms "first", "second", and the like in the description and in the claims of this application are used for distinguishing between similar objects and not necessarily for describing a particular sequential or chronological order. It is to be understood that the use of a singular term like "image" can include a plural of the singular term so that when the singular is used herein it can mean the singular or it can also include the plural. The term "some" when used herein can include one or more. The term "example" is used herein to mean "serving as an example, instance, or illustration." Any implementation of the application that is discussed in this application is not meant to be an exhaustive list of implementations, use, or applications for the subject matter described in this application. It is to be understood that this application is

[0041] To facilitate understanding of the technical solutions provided by the embodiments of the present application, some key terms used by the embodiments of the present application are explained first:

[0042] 1、XML: a markup language used to mark electronic files to have structural, extensible markup language, a subset of standard general markup language.

[0043] 2、DOM, SAX, JDOM, DOM4J: DOM and SAX are the ways of Java native support for XML data parsing, JDOM and DOM4J are open source components for simplifying Java and XML interaction. Specifically, the DOM4J involved in the present application is a Java XML API, which is an upgrade of JDOM, and is used to read and write XML files.

[0044] 3、FasterXML: FasterXML jackson-databind is a library based on JAVA that can convert XML and JSON data formats and JAVA objects. Jackson can easily convert Java objects into json objects and XML documents, and also can convert json, xml into Java objects.

[0045] 4、Document file object: the Java object generated by the DOM4J component when reading and writing XML files.

[0046] 5、Element element object: each XML data node in the Document object is parsed into an Element object; wherein the entire Element object is a tree structure, and the root node is the root node in the XML data. Specifically, the root node can be obtained by using the getRootElement method of the Document object, and the entire Element object tree can be traversed by using this method.

[0047] 6、Path: the path in the XML data from the root node to the data node in the embodiment of the present application.

[0048] 7, Note: Java language support for metadata, annotations are special marks in the code, these marks can be read at compile, class loading, runtime, and perform corresponding processing.

[0049] The design idea of the embodiment of the application is briefly introduced as follows:

[0050] At present, in the financial industry, the entire transaction process of electronic commercial bills is realized by the message communication between each member institution and the exchange. Since the message body in the message is in XML format, and there are many types of messages and deep levels, how to parse into Java objects used by the system, and how to convert the processed Java objects into XML format data, become problems to be solved.

[0051] To solve this problem, related technologies provide a solution that can use Java parsing components such as DOM, SAX, JDOM, DOM4J, etc. to parse XML data into related data objects, and then convert the data objects into custom Java objects. However, when the XML data format is not fixed, each XML data needs to be manually parsed and mapped, that is, this hard parsing method has poor scalability, resulting in low conversion efficiency of XML data and Java objects, and further affecting the processing efficiency of the transaction process.

[0052] In addition, related technologies also provide an open source component such as FasterXML, which can parse and complete the mapping of XML data and Java objects. However, in this solution, each XML data needs to generate a corresponding Java object, and when the XML node level is deep, the data object will be complex, and there are security vulnerabilities.

[0053] For example, refer to Figure 1 As shown in the figure, Figure 1 A figure in the A object is XML data, Figure 1 The Java object corresponding to the XML data in B figure is shown in the figure, wherein the level of the Java object is four, and three inner classes are defined inside the A object. Obviously, when the A object is assigned, the code will be very bloated. Moreover, open source components, especially parsing class components, have many security vulnerabilities, and strict dependence on open source component programs has high subsequent maintenance risk.

[0054] In view of this, the application provides a data conversion method, which can complete the data mapping relationship between the Document object and the custom Java object through a self-designed rule data intermediate layer Map, and realize the parsing and mapping of XML data and Java objects.

[0055] After introducing the design idea of the embodiment of the present application, the application scenarios to which the data conversion technical solution in the embodiment of the present application is applicable are briefly introduced as follows. It should be noted that the application scenarios described in the embodiment of the present application are for more clearly explaining the technical solution of the embodiment of the present application, and do not constitute a limitation on the technical solution provided by the embodiment of the present application. It can be known by those skilled in the art that, with the appearance of new application scenarios, the technical solution provided by the embodiment of the present application is also applicable to similar technical problems.

[0056] In the embodiment of the present application, please refer to the application scenario schematic diagram shown in FIG. 1, which includes an electronic device 201 and a computer device 202. The electronic device 201 can communicate with the computer device 202, for example, directly or indirectly connected through wired or wireless communication mode, which is not limited by the present application. Figure 2

[0057] In this scenario, the electronic device 201 can send XML data to the computer device 202, and the computer device 202 converts and processes the XML data to obtain the corresponding Java object and feeds back the Java object to the electronic device 201.

[0058] The computer device 202 can be an independent physical server, a server cluster or a distributed system composed of multiple physical servers, a cloud server providing cloud services, cloud databases, cloud computing, cloud functions, cloud storage, network services, cloud communication, middleware services, domain name services, security services, CDN (Content Delivery Network), and basic cloud computing services such as big data and artificial intelligence platforms. The electronic device 201 can be a smart phone, a tablet computer, a notebook computer, a desktop computer, a smart television, a smart wearable device, or a server, but is not limited thereto.

[0059] To further illustrate the data conversion method provided by the embodiment of the present application, the following will be described in detail in combination with the drawings and specific embodiments. Although the embodiment of the present application provides the method operation steps as shown in the following embodiments or drawings, more or fewer operation steps can be included in the method based on conventional or non-creative labor. The execution order of these steps is not limited to the execution order provided by the embodiment of the present application in the steps that do not have necessary causal relationship in logic. The method can be executed in sequence or in parallel (for example, parallel processor or multi-threaded application environment) when actually processing or executed by the device, as shown in the embodiments or drawings.

[0060] The following will be described in combination with Figure 3 ​The method flowchart shown illustrates the data conversion method in the embodiments of the present application. The method flow of the embodiments of the present application is described below.

[0061] Step 301: reading the XML data to be converted to obtain a first file object.

[0062] In the embodiments of the present application, the electronic device can send the XML data to be converted to the computer device, and then the computer device can read the XML data to be converted based on a preset parsing component to obtain a Document object (i.e., the first file object). The preset parsing component can be DOM4J.

[0063] Step 302: parsing the first file object to obtain first custom Map structure data; the first custom Map structure data includes a key value determined based on a node path and a node property and a value value corresponding to the key value.

[0064] In the embodiments of the present application, after obtaining the first file object, the computer device can parse the first file to obtain the first custom Map structure data.

[0065] In the embodiments of the present application, the first custom Map structure data can be determined by, but not limited to, the following steps:

[0066] Step A: parsing the first file object to obtain a root node of the first file object and a child node of the root node.

[0067] In the embodiments of the present application, the computer device performs a depth-first traversal process on the first file object, recursively obtains a root node root and all child nodes children of the root node root of the Document object, and further determines the paths of the child nodes, wherein the paths are determined based on a custom rule.

[0068] For example, please refer to Figure 4 , Figure 4 the schematic diagram of the paths shown in the embodiments of the present application. Specifically, each path from the root node A to each child node is called an XML path, which is referred to as a root path hereinafter. In addition, the path separator in the embodiments of the present application uses an English period. For example, Figure 4 the path of the AB child node is A.AB, and the node path of the AAC child node is A.AA.AAA.AAC. In addition, Figure 4 a node without child nodes (i.e., degree 0) in Figure 4 the last layer of child nodes AAA and the child node AAA in are homonymous nodes.

[0069] Step B: traversing the child nodes of the root node to determine the key value corresponding to each child node and the value value corresponding to the key value to determine the first custom Map structure data.

[0070] In the embodiment of the application, when it is determined that any child node is an attribute node, the any child node is added to the first custom Map structure data, and the key value corresponding to the any child node includes a root path, a node name, a distinguisher and an attribute name, and the value value corresponding to the key value is an attribute value.

[0071] For example, when it is determined that the child node 1 is an attribute node, the child node 1 is added to the first custom Map structure data, and the key value corresponding to the child node 1 can be expressed as: root path+this node name+ <attr>+ attribute name, such as A.AA. <attr>.name, and the value corresponding to the key value is the attribute value corresponding to the child node 1.

[0072] In the embodiment of the present application, when it is determined that any child node is a leaf node and the key value corresponding to the child node does not exist in the first custom Map structure data, the child node is added to the first custom Map structure data, and the key value corresponding to the child node includes a root path and a node name, and the value corresponding to the key value is a node data value.

[0073] For example, when the child node 2 is a leaf node, the child node 2 is added to the first custom Map structure data, and the key value corresponding to the child node 2 can be represented as: root path+this node name, such as A.AA, and the value corresponding to the key value is the node data value.

[0074] In addition, when it is determined that any child node is a non-leaf node, the root node is returned for recursive processing, and the root path is set as the current path corresponding to the child node for traversal processing.

[0075] In the embodiment of the present application, when it is determined that any child node is a leaf node and the key value corresponding to the child node exists in the first custom Map structure data, the child node is added to the first custom Map structure data, and the key value corresponding to the child node includes a root path, a node name and an index identifier, and the value corresponding to the key value is a node data value.

[0076] For example, when the child node 3 is a leaf node and the key value corresponding to the child node 3 exists in the first custom Map structure data, the child node 3 is added to the first custom Map structure data, and the key value corresponding to the child node 3 can be represented as: root path+this node name, and the key value is indexed, such as A.AA[1], and the value corresponding to the key value is the node data value.

[0077] In the embodiment of the present application, when the child nodes are processed, the key values and the value corresponding to the nodes are added to the first custom Map structure to obtain the first custom Map structure data.

[0078] Step 303: Determine the attribute annotation information of the custom Java object corresponding to the first file object, and determine the mapping key value corresponding to the custom Java object based on the attribute annotation information.

[0079] In the embodiment of the present application, the computer device can also determine the attribute annotation information of the custom Java object corresponding to the first file object. The attribute annotation information includes path information, same-name node information and XML attribute information.

[0080] Please refer to Figure 5 , Figure 5 An exemplary attribute annotation information diagram is provided for the embodiments of the present application. In the diagram, Figure 5 The WeXML in the diagram can be understood as attribute annotation information, which includes path, index, and isAttr. Specifically, the path is an XML node path (such as A.AA.AAA.AAAC), the index is a same-name node subscript, and the isAttr is whether it is an XML attribute, and the default is no.

[0081] As can be seen, in the embodiments of the present application, the attribute annotation information can be used to mark which position of the XML data the attribute of the Java object is mapped to, that is, the path in the annotation is used to mark, and the custom path mapping is realized. That is, through the custom designed path mapping rule, the attribute of the Java object is supported to be mapped into the attribute of the node in the XML data. In addition, through the aforementioned attribute annotation information, the problem that the XML data allows the same-name node to exist, but the attributes in the Java object cannot be renamed can also be solved, that is, a set of usable annotation information mapping into the key in the Map is designed in the embodiments of the present application.

[0082] In the embodiments of the present application, the following several ways can be used to determine the mapping key value:

[0083] Method 1:

[0084] In the embodiments of the present application, when it is determined that the same-name node information does not exist in the attribute annotation information, and the XML attribute information does not exist in the attribute annotation information, it is determined that the mapping key value includes the path.

[0085] For example, the path information in the attribute annotation information is determined, if the index is equal to 0, and the isAttr is false, then the mapping key value is equal to the path.

[0086] Method 2:

[0087] In the embodiments of the present application, when it is determined that the same-name node information exists in the attribute annotation information, and the XML attribute information does not exist in the attribute annotation information, it is determined that the mapping key value includes the path and the same-name node subscript in the preset format.

[0088] For example, the path information in the attribute annotation information is determined, if the index is greater than 0, and the isAttr is false, then the mapping key value is equal to the path + [index], such as A.AA[1].

[0089] Method 3:

[0090] In the embodiment of the present application, when it is determined that the same name node information does not exist in the attribute annotation information, and the XML attribute information exists in the attribute annotation information, it is determined that the mapping key value comprises the path, the distinguisher and the attribute name.

[0091] For example, the path path information in the attribute annotation information is determined, if the index is equal to 0, and the isAttr is true, then the mapping key value is equal to the path+ <attr>+ attribute name, e.g. A.AA. <attr>.name.

[0092] Mode 4:

[0093] When it is determined that there is same name node information in the attribute annotation information, and there is XML attribute information in the attribute annotation information, it is determined that the mapping key value includes the path, the distinguisher, the attribute name, and the subscript distinguisher.

[0094] For example, when it is determined that the path path information in the attribute annotation information, if the index is greater than 0, and isAttr is true, then the mapping key is equal to path+ <attr>+ attribute name [index], e.g. mapping key value: A.AA. <attr>.name[1].

[0095] Step 304: searching the matching value value matched with the mapping key value in the first custom Map structure data, and reflecting and assigning the matching value value to the custom JAVA object, so as to obtain the target Java object corresponding to the XML data to be converted.

[0096] In the embodiment of the application, only the Java object corresponding to the XML needs to be defined, and the entire mapping and parsing process is automatically completed, thereby greatly improving the efficiency of converting different format XML data into Java objects.

[0097] Optionally, the attribute annotation information corresponding to the custom Java object is updated based on the XML data to be converted. It can be seen that in the embodiment of the application, the computer device can parse the custom annotation for the Java object attribute into XML, that is, determine which position of the Java object attribute is mapped to the XML data, and the path in the annotation is marked, thereby realizing custom path mapping. Moreover, due to the different formats of the XML data, the corresponding attribute annotation information is also different.

[0098] Referring to FIG. 1, Figure 6 , Figure 6 is a schematic diagram of converting XML data into a Java object provided in the embodiment of the application. The parser can perform depth-first traversal of all element nodes, and save the path information from the root node to the leaf node. For example, the path information is divided by the English period, that is, the small dot. The Key value in the first custom structure Map data is the Document node path, and the value value is the node data information. In addition, the mapper can parse the attribute annotation information of the custom Java object, then obtain the corresponding value value according to the path in the attribute annotation information, and finally assign the corresponding value value to the Java object through reflection processing to obtain the target Java object.

[0099] It can be seen that through a series of custom parsing and mapping rules, a custom Map structure data, and Java annotation, reflection and other methods, the generation and data injection of the XML data into the Java object are automatically completed.

[0100] In the embodiment of the application, after the computer device obtains the target Java object, the target Java object can also be operated to obtain a new Java object. It should be noted that the operation processing of the target Java object can be determined based on actual implementation, and the embodiment of the application does not limit this.

[0101] In the embodiment of the present application, the computer device can obtain the annotation attribute information corresponding to the new Java object by reflecting the new Java object, and store the path information and data in the annotation attribute information into the second custom Map structure data. Further, the computer device can create a second file object, and complete the establishment of the object tree corresponding to the second file object based on the path information and data in the second custom Map structure data. Then, the computer device can perform reverse analysis on the object tree by using the preset plug-in, and generate the target XML data corresponding to the new Java object.

[0102] For example, in the process of completing the establishment of the object tree corresponding to the second file object, the following steps can be used, but are not limited to:

[0103] Step a: loop through the second custom Map structure data to obtain the path information of the root node and the child node.

[0104] In the embodiment of the present application, the root node can be set as root, and then the path information with the path equal to the key value in the second custom Map structure data is obtained, so as to analyze the path information to determine the parent node and the child node.

[0105] The preset analysis rule can be to intercept the path from 0 to the first delimiter in the path information as the parent path, and set the path from the first delimiter to the end of the path information as the child path. In the following, the parent path can be referred to as ppath, and the child path can be referred to as cpath.

[0106] For example, assuming that the path information is A.AA.AAA path, A can be determined as ppath, and AA.AAA can be determined as cpath.

[0107] Specifically, the node corresponding to ppath can be found or generated under the root node. If the node corresponding to ppath exists under the root node, the ppath node at this time is taken as the child node, the path information is cpath, and the value corresponding to the node is determined. If the node corresponding to ppath does not exist under the root node, the node corresponding to ppath is newly created, the node corresponding to ppath is the parent node, and the value corresponding to the node is determined.

[0108] Optionally, when ppath is <attr>If the information corresponding to the parent path is a distinguisher, the attribute name of the current node can be set as cpath, and the data value corresponding to the node is value. That is, the current node is an attribute node.

[0109] Step b: when all data in the second custom Map structure data is traversed, the Element object tree with root as the root node is generated.

[0110] In the embodiment of the present application, when there is no path under each cpath, the root node and the leaf node and the non-leaf node are all generated, thereby obtaining the Element object tree.

[0111] It can be seen that if there is a parent node in the path information, the node is set after the parent node, that is, it is mounted under the parent node, and if there is no parent node in the path information, the child node in the path information is determined as the parent node. Therefore, when the data in the second custom Map structure data is traversed, that is, when all path information is parsed, the corresponding Element object tree can be obtained.

[0112] Referring to Figure 7 , Figure 7 is a schematic diagram of the target XML data obtained by converting the new Java object in the embodiment of the present application. The object parser is used to parse all fields and values of the Java object, and the path information in the field corresponding to the attribute annotation information (for example, called @WeXML), and then store the path information and the value into the second custom Map structure data. Therefore, the key value in the second custom Map structure data is the data path information, and the value is the data information. Further, the node generator can traverse the second custom Map structure data, and then generate the tree-shaped node, that is, the element object, recursively according to the path information, that is, the key value, so as to fill the data value in the second custom Map structure data into the corresponding leaf node, and fill the attribute information into the corresponding node attribute information, and then fill the entire node data into the newly created second file object, that is, the Document object. Therefore, the target XML data can be obtained.

[0113] It can be seen that in the embodiment of the present application, the Element object tree can be reversely constructed by the rule customized in the conversion from the XML data to the Java object, thereby the Element object tree is parsed, and the new XML data is obtained. It is obvious that the data conversion scheme provided in the embodiment of the present application can realize the mutual conversion between the XML data and the Java object in various formats.

[0114] In order to better explain the conversion process of the XML data and the Java object provided by the embodiment of the present application, the following specific examples are given.

[0115] Specifically, when the XML data has a node attribute, for example:

[0116] <ab>

[0117] <AAAA name="test">aaaa

[0118] < / ab>

[0119] When the XML data is parsed into the first self-defined Map structure data, it is automatically mapped into the following data:

[0120] 1. key = AB.AAAA. <attr>.name, value=test;

[0121] 2. key=AB.AAAA, value=aaaa;

[0122] Therefore, in the object reverse resolution value, the first custom Map structure data will be automatically found in the key value: AB.AAAA. <attr>Data information of.name and AB.AAAA.

[0123] Specifically, for the first custom Map structure data, the key value includes path information, and the key in the first custom Map structure data is unique. However, in the XML data, nodes with the same name are allowed to exist. For this problem, in the XML forward parsing, array subscripts are automatically added, such as A.AA.AAA.AAAB[0] and A.AA.AAA.AAAB[1].

[0124] For the Java object, the index needs to be indicated in the annotation, so in the value searching in the Java object reverse parsing, the data information with the key value of A.AA.AAA.AAAB[0] and A.AA.AAA.AAAB[1] in the first custom Map structure data is automatically searched.

[0125] As shown in Figure 8 The application provides a data conversion device, which comprises: a first obtaining unit 801, which is used for reading XML data to be converted and obtaining a first file object; a first processing unit 802, which is used for parsing the first file object and obtaining first custom Map structure data; the first custom Map structure data comprises a key value determined based on a node path and a node characteristic and a value value corresponding to the key value; a second processing unit 803, which is used for determining attribute annotation information of a custom Java object corresponding to the first file object and determining a mapping key value corresponding to the custom Java object based on the attribute annotation information; a second obtaining unit 804, which is used for searching a matching value value in the first custom Map structure data that matches the mapping key value, reflecting and assigning the matching value value to the custom JAVA object, and obtaining a target Java object corresponding to the XML data to be converted.

[0126] Optionally, the device further comprises a third processing unit, which is used for: obtaining a new Java object after operation on the target Java object; obtaining annotation attribute information corresponding to the new Java object through reflection processing on the new Java object, and storing path information and data in the annotation attribute information into second custom Map structure data; creating a second file object, establishing an object tree corresponding to the second file object based on the path information and data in the second custom Map structure data; and generating target XML data corresponding to the new Java object through reverse parsing processing on the object tree by a preset plug-in.

[0127] Optionally, the third processing unit is specifically configured to: based on the path information and the data in the second custom Map structure data, complete establishment of an object tree corresponding to the second file object, including: determining a root node corresponding to the path information in the second custom Map structure data; based on a preset analysis rule, analyzing each path information in the second custom Map structure data, respectively obtaining a corresponding parent path and a child path, taking a node corresponding to the child path as a child node, and determining a value corresponding to the child node; when information corresponding to the parent path is not a distinguisher, determining whether the root node includes a node corresponding to the parent path; when it is determined that the root node includes the node corresponding to the parent path, taking the node corresponding to the parent path as a child node, and determining a value corresponding to the child node; when it is determined that the root node does not include the node corresponding to the parent path, taking the node corresponding to the parent path as a parent node, and determining a value corresponding to the parent node; when information corresponding to the parent path is a distinguisher, determining that the node corresponding to the parent path is an attribute node, and a data value corresponding to the parent path is a corresponding value; based on the parent node, the child node, and the attribute node, and value corresponding thereto, constructing the object tree corresponding to the second file object.

[0128] Optionally, the first processing unit 802 is specifically configured to: parse the first file object to obtain a root node of the first file object and a child node of the root node; traverse the child node of the root node to determine a key value corresponding to each child node and a value corresponding to the key value, to determine first custom Map structure data.

[0129] Optionally, the first processing unit 802 is specifically configured to: when determining that any of the sub-nodes is an attribute node, adding any of the sub-nodes to the first custom Map structure data, and determining that a key value corresponding to any of the sub-nodes comprises a root path, a node name, a distinguisher and an attribute name, and a value corresponding to the key value is an attribute value; when determining that any of the sub-nodes is a leaf node and any of the sub-nodes does not exist in the first custom Map structure data, adding any of the sub-nodes to the first custom Map structure data, and determining that a key value corresponding to any of the sub-nodes comprises a root path and a node name, and a value corresponding to the key value is a node data value; and when determining that any of the sub-nodes is a leaf node and any of the sub-nodes exists in the first custom Map structure data, adding any of the sub-nodes to the first custom Map structure data, and determining that a key value corresponding to any of the sub-nodes comprises a root path, a node name and a subscript distinguisher, and a value corresponding to the key value is a node data value.

[0130] Optionally, the second processing unit 803 is configured to: when determining that the attribute annotation information does not exist in the same name node information and the XML attribute information does not exist in the attribute annotation information, determining that a mapping key value comprises a path; when determining that the attribute annotation information exists in the same name node information and the XML attribute information does not exist in the attribute annotation information, determining that a mapping key value comprises a path and a same name node serial number in a preset format; when determining that the attribute annotation information does not exist in the same name node information and the XML attribute information exists in the attribute annotation information, determining that a mapping key value comprises a path, a distinguisher and an attribute name; and when determining that the attribute annotation information exists in the same name node information and the XML attribute information exists in the attribute annotation information, determining that a mapping key value comprises a path, a distinguisher, an attribute name and a subscript distinguisher.

[0131] Optionally, attribute annotation information corresponding to the custom Java object is updated based on the to-be-converted XML data.

[0132] The embodiment of the present application provides a computer device, which comprises a program or an instruction, and when the program or the instruction is executed, a data conversion method and any optional method provided by the embodiment of the present application are executed.

[0133] The embodiment of the present application provides a storage medium, which comprises a program or an instruction, and when the program or the instruction is executed, a data conversion method and any optional method provided by the embodiment of the present application are executed.

[0134] Finally, it should be understood that the embodiments of the present application can be provided as a method, a system, or a computer program product. Accordingly, the present application can be embodied in the form of an entirely hardware embodiment, an entirely software embodiment, or an embodiment combining software and hardware aspects. Furthermore, the present application can take the form of a computer program product on one or more computer-usable storage media (including, but not limited to, disk storage, CD-ROM, optical storage, and so forth) embodying computer-readable program code.

[0135] The present application is described in reference to the flowchart illustrations and / or block diagrams of the methods, apparatus (systems) and computer program products according to the present application. It will be understood that each block of the flowchart illustrations and / or block diagrams, and combinations of blocks in the flowchart illustrations and / or block diagrams, can be implemented by computer program instructions. These computer program instructions can be provided to a processor of a general purpose computer, special purpose computer, embedded processor, or other programmable data processing apparatus to produce a machine, such that the instructions, which execute via the processor of the computer or other programmable data processing apparatus, create means for implementing the functions specified in the flowchart illustrations and / or block diagrams. Figure 1 one or more functions specified in one or more of the flowchart illustrations and / or block diagrams. Figure 1 means for performing the functions specified in one or more of the flowchart illustrations and / or block diagrams.

[0136] These computer program instructions can also be stored in a computer- readable memory that can direct a computer or other programmable data processing apparatus to function in a particular manner, such that the instructions stored in the computer-readable memory produce an article of manufacture including instructions which implement the functions specified in the flowchart illustrations and / or block diagrams. Figure 1 one or more functions specified in one or more of the flowchart illustrations and / or block diagrams. Figure 1 Figure 1 means for performing the functions specified in one or more of the flowchart illustrations and / or block diagrams.

[0137] Obviously, numerous modifications and variations of the present application are possible in light of the above teachings. It is therefore to be understood that within the scope of the claims and their equivalents, the present application can be practiced otherwise than as specifically described.< / attr> < / attr> < / attr> < / attr> < / attr> < / attr> < / attr> < / attr> < / attr>

Claims

1. A data conversion method, characterized in that, include: Read the XML data to be converted to obtain the first file object; The first file object is parsed to obtain the root node and child nodes of the first file object; The child nodes of the root node are traversed to determine the key value and the value corresponding to the key value of each child node, so as to determine the first custom Map structure data; The first custom Map structure data includes a key value determined based on node path and node characteristics, and a value corresponding to the key value; Determine the attribute annotation information of the custom Java object corresponding to the first file object, and determine the mapping key value corresponding to the custom Java object based on the attribute annotation information; Find the matching value in the first custom Map structure data that matches the mapping key value, and assign the matching value to the custom Java object by reflection to obtain the target Java object corresponding to the XML data to be converted.

2. The method as described in claim 1, characterized in that, After obtaining the target Java object corresponding to the XML data to be converted, the method further includes: Obtain a new Java object after operations on the target Java object; By performing reflection processing on the new Java object, the annotation attribute information corresponding to the new Java object is obtained, and the path information and data in the annotation attribute information are stored in the second custom Map structure data; Create a second file object, and based on the path information and data in the second custom Map structure data, complete the construction of the object tree corresponding to the second file object; The object tree is reverse-parsed using a preset plugin to generate the target XML data corresponding to the new Java object.

3. The method as described in claim 2, characterized in that, Based on the path information and data in the second custom Map structure, the object tree corresponding to the second file object is established, including: Determine the root node corresponding to the path information in the second custom Map structure data; Based on preset analysis rules, the path information in the second custom Map structure data is analyzed to obtain the corresponding parent path and child path, and the node corresponding to the child path is taken as the child node, and the value corresponding to the child node is determined. When the information corresponding to the parent path is not a distinguisher, determine whether the root node includes the node corresponding to the parent path; When it is determined that the root node includes the node corresponding to the parent path, the node corresponding to the parent path is taken as a child node, and the value corresponding to the child node is determined; when it is determined that the root node does not include the node corresponding to the parent path, the node corresponding to the parent path is taken as the parent node, and the value corresponding to the parent node is determined. When the information corresponding to the parent path is a distinguisher, the node corresponding to the parent path is determined to be an attribute node, and the data value corresponding to the parent path is the corresponding value. Based on the parent node, child node, and attribute node, and their respective value, construct the object tree corresponding to the second file object.

4. The method as described in claim 1, characterized in that, The child nodes of the root node are traversed to determine the key value and the value corresponding to the key value of each child node, thereby determining the first custom Map structure data, including: When any of the child nodes is determined to be an attribute node, the child node is added to the first custom Map structure data, and the key value corresponding to any of the child nodes is determined to include the root path, node name, distinguisher and attribute name, and the value value corresponding to the key value is the attribute value. When any of the child nodes is determined to be a leaf node, and no key value exists for any of the child nodes in the first custom Map structure data, then any of the child nodes is added to the first custom Map structure data, and the key value corresponding to any of the child nodes is determined to include the root path and the node name, and the value value corresponding to the key value is the node data value. When any of the child nodes is determined to be a leaf node, and any of the child nodes has a key value in the first custom Map structure data, then any of the child nodes is added to the first custom Map structure data, and the key value corresponding to any of the child nodes is determined to include the root path, node name and subscript distinguisher, and the value value corresponding to the key value is the node data value.

5. The method as described in claim 1, characterized in that, Based on the attribute annotation information, the mapping key value corresponding to the custom Java object is determined, including: If it is determined that there is no node with the same name in the attribute annotation information and there is no XML attribute information in the attribute annotation information, then the mapping key value is determined to include the path; When it is determined that there is a node with the same name in the attribute annotation information, and there is no XML attribute information in the attribute annotation information, the mapping key value is determined to include the path and the sequence number of the node with the same name in a preset format. When it is determined that there is no node with the same name in the attribute annotation information, and there is XML attribute information in the attribute annotation information, the mapping key value is determined to include the path, the distinguisher, and the attribute name; When it is determined that there is a node with the same name in the attribute annotation information and there is XML attribute information in the attribute annotation information, the mapping key value is determined to include the path, the distinguisher, the attribute name, and the subscript distinguisher.

6. The method according to any one of claims 1-5, characterized in that, The attribute annotation information corresponding to the custom Java object is updated based on the XML data to be converted.

7. A data conversion device, characterized in that, include: The first obtaining unit is used to read the XML data to be converted and obtain the first file object; The first processing unit is used to parse the first file object to obtain the root node and child nodes of the first file object. The child nodes of the root node are traversed to determine the key value and the value corresponding to the key value of each child node, so as to determine the first custom Map structure data; The first custom Map structure data includes a key value determined based on node path and node characteristics, and a value corresponding to the key value; The second processing unit is used to determine the attribute annotation information of the custom Java object corresponding to the first file object, and to determine the mapping key value corresponding to the custom Java object based on the attribute annotation information. The second obtaining unit is used to find the matching value in the first custom Map structure data that matches the mapping key value, and to reflectively assign the matching value to the custom JAVA object to obtain the target Java object corresponding to the XML data to be converted.

8. A computer device, characterized in that, Includes a program or instructions, which, when executed, cause the method described in any one of claims 1 to 6 to be performed.

9. A storage medium, characterized in that, Includes a program or instructions, which, when executed, cause the method described in any one of claims 1 to 6 to be performed.

Citation Information

Patent Citations

  • Method for binding Java and XML data

    CN101699397A

  • XML file analysis method and related equipment

    CN111753140A