Structured exchange text generation method and system
By loading structured text templates into SQL query statements and using embedded functions to generate XML/JSON formatted data exchange text, the problem of complex and inefficient generation processes in existing technologies is solved, achieving efficient and simplified data conversion.
Patent Information
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- FOUNDER INT(WUHAN)TECH DEV CO LTD
- Filing Date
- 2025-12-24
- Publication Date
- 2026-05-12
AI Technical Summary
In existing technologies, the process of generating data exchange files in XML/JSON format is complex and inefficient, requiring cumbersome intermediate conversion code and relying on third-party libraries.
By loading a structured text template into an SQL query statement and using SQL embedded functions to directly embed data fields into the template, structured exchange text is generated, and reserved characters are automatically replaced to form the final format.
It simplifies the generation process, reduces the amount of development code, improves data conversion efficiency, does not rely on third-party libraries, adapts to various format requirements, and is easy to maintain.
Smart Images

Figure CN122018864A_ABST
Abstract
Description
Technical Field
[0001] This invention belongs to the field of computer software technology, and in particular relates to a method and system for generating structured exchange text. Background Technology
[0002] In the field of computer application software, data storage and exchange are crucial functions of software systems. To facilitate business operations such as adding, deleting, modifying, and querying data, large volumes of structured data are commonly stored using relational databases. When exchanging this structured data between different application systems, a data carrier is required. In the early days, due to limited computer network bandwidth and storage capacity, binary messages or binary files were widely used for transmission, but these methods suffered from poor compatibility and readability. With the rapid development of hardware, the bottleneck of excessively pursuing information size and efficiency has been overcome. Currently, the carrier used for data exchange between systems has become structured text, such as CSV, XML, JSON, and custom structured text. Among these, XML and JSON formats are widely used as carriers for data exchange between independent systems due to their strong readability, ease of development and debugging, and good compatibility and scalability.
[0003] Due to the need for data exchange between systems, for data providers, retrieving data already stored in a relational database, reorganizing and converting it to a structured text format such as XML or JSON is an essential function. For data recipients, reading / parsing structured text such as XML / JSON, extracting the data, and storing it in their own relational database or directly using the data is also an essential function.
[0004] Currently, the most common method is to retrieve data from relational databases and convert it to XML / JSON format. This involves using SDK functions provided by the database access driver in the programming language to execute SQL statements to access the database, retrieve data that meets business requirements, and create records in memory, each with corresponding fields—essentially a data table in memory. Then, the programming language calls the corresponding XML / JSON read / write library functions to populate the data from each field of the data table into the nodes of the XML / JSON tree structure, following a trunk-branch-leaf structure. Finally, the library functions export the entire tree structure as structured text. Clearly, this method of reading data and generating XML / JSON format is complex. Since the result of an SQL query is a two-dimensional table structure, while XML / JSON is a tree structure, the intermediate data conversion code is cumbersome to develop. Furthermore, each node in the XML / JSON tree needs to be accessed and its attributes and values filled in one by one, resulting in low conversion efficiency. Summary of the Invention
[0005] In view of this, embodiments of the present invention provide a method and system for generating structured exchange text, which solves the problems of complex construction process and low data conversion efficiency of current exchange files.
[0006] In a first aspect of the present invention, a method for generating structured exchange text is provided, comprising: Load the template of the structured text and construct the corresponding SQL query statement for the database; Execute SQL query statements, concatenate the query results in order, and generate structured exchange text; If the query result of the SQL statement contains structured text reserved words, an automatic replacement function is added to the corresponding field in the SQL query statement to replace the structured text reserved words with legal escape characters.
[0007] In a second aspect of the present invention, a structured exchange text generation system is provided, comprising: The query statement construction module is used to load structured text templates and construct corresponding database SQL query statements; The exchange text generation module is used to execute SQL query statements, concatenate the query results of the SQL statements in sequence, and generate structured exchange text. If the query result of the SQL statement contains structured text reserved words, an automatic replacement function is added to the corresponding field in the SQL query statement to replace the structured text reserved words with legal escape characters.
[0008] In a third aspect of the present invention, an electronic device is provided, including a memory, a processor, and a computer program stored in the memory and executable by the processor, wherein the processor executes the computer program to implement the steps of the method as described in the first aspect of the present invention.
[0009] In a fourth aspect of the present invention, a computer-readable storage medium is provided, the computer-readable storage medium storing a computer program, which, when executed by a processor, implements the steps of the method provided in the first aspect of the present invention.
[0010] In this embodiment of the invention, based on the string processing capabilities of the SQL query language, the structured text template is converted into an SQL query statement. After executing the database query, the query results are concatenated to form structured text for data exchange. This not only simplifies the business processing and reduces the amount of development code, but also achieves high data conversion efficiency. It does not rely on third-party read / write libraries, which can greatly improve development and execution efficiency, and is also easy to maintain. Attached Figure Description
[0011] To more clearly illustrate the technical solutions in the embodiments of the present invention, the drawings used in the description of the embodiments or the prior art will be briefly introduced below. Obviously, the drawings described below are only some embodiments of the present invention. For those skilled in the art, other drawings can be obtained based on these drawings without creative effort.
[0012] Figure 1 This is a flowchart illustrating a structured exchange text generation method according to an embodiment of the present invention. Figure 2 This is a schematic diagram of the structure of a structured text generation system according to an embodiment of the present invention; Figure 3 This is a schematic diagram of the structure of an electronic device provided in one embodiment of the present invention. Detailed Implementation
[0013] To make the objectives, features, and advantages of this invention more apparent and understandable, the technical solutions of the embodiments of this invention will be clearly and completely described below with reference to the accompanying drawings. Obviously, the embodiments described below are only some embodiments of this invention, and not all embodiments. Based on the embodiments of this invention, all other embodiments obtained by those skilled in the art without creative effort are within the scope of protection of this invention.
[0014] It should be understood that the terms "comprising" and other similar expressions in the specification, claims, and accompanying drawings of this invention are intended to cover a non-exclusive inclusion, such as a process, method, system, or apparatus that includes a series of steps or units and is not limited to the listed steps or units. Furthermore, "first" and "second" are used to distinguish different objects and are not intended to describe a specific order.
[0015] Understandably, for structured data such as XML, the commonly used query methods currently involve writing SQL statements that specify the fields to be queried, which tables to use, and what conditions to meet. The typical syntax is: "select ID,name,address,tel,comment from hospital order by ID asc;".
[0016] The query results are logically structured as a two-dimensional table in memory, but the XML format usually exchanged externally is in the form of individual query records.
[0017] It is evident that there is a significant difference between the query results in memory and the final XML format required. Traditional methods can only achieve this conversion by developing a separate conversion module and utilizing a third-party XML access library, a process that is cumbersome and inefficient. Clearly, existing SQL query methods still suffer from cumbersome processes and low execution efficiency.
[0018] In view of this, some embodiments of the present invention propose a new method for generating structured exchange text to simplify the exchange text generation process and improve conversion efficiency.
[0019] Please see Figure 1 The present invention provides a flowchart illustrating a method for generating structured exchange text, comprising: S101. Load the template of the structured text and construct the corresponding database SQL query statement; Structured text is a data format with a clearly defined format and hierarchical structure, generally used to represent complex information. This type of text can be parsed and manipulated using predefined patterns or rules. Structured text formats can include JSON, XML, CSV, etc. The template for structured text contains predefined text formatting information. SQL (Structured Query Language) queries are instructions used to retrieve data from a database; they can perform content retrieval based on fields, conditions, etc.
[0020] This involves loading the structured text template into the SQL query language and embedding the pre-queried fields from the structured text template into the corresponding positions in the SQL query statement.
[0021] By utilizing the built-in string processing functions in the SQL language, the format template of a structured file can be directly loaded into the SQL query language. The fields to be queried in the structured template can be embedded into the SQL query statement. Executing the SQL query will inject the query results into the XML template by field, and a record will become an item in the XML.
[0022] For example, the query statement generated after loading the XML template in SQL becomes: select concat(' <record> <id> ',ID,'< / id> <name> ', name,'< / name> <address> ', address,'< / address> <tel> ', tel,'< / tel> <comment> ',comment,'< / comment> < / record> ') from hospital order by ID asc; `concat` is a SQL-embedded string concatenation function. The string enclosed in single quotes is a fixed string, which is the decomposed XML template, and the string sandwiched in the middle is the database field.
[0023] The query results of the above SQL query are converted into a two-dimensional table with only one column in memory: <record> <id> 010001< / id> <name> Hospital A< / name> <address> No. 49, xx Road, xx City< / address> <tel> 010-82266698< / tel> <comment> Hospital A was founded in 1958 and is a comprehensive tertiary hospital integrating medical treatment, teaching, scientific research, preventive healthcare, rehabilitation and health management.< / comment> < / record> <record> <id> 010002< / id> <name> Hospital B< / name> <address> No. 8, xx Street, xx City< / address> <tel> 010-83572212< / tel> <comment> Hospital B, located in area B, is a large-scale comprehensive tertiary-level Class A hospital integrating medical treatment, teaching, scientific research, and prevention. It serves as a healthcare base hospital for area B.< / comment> < / record> <record> <id> 010003< / id> <name> C Hospital< / name> <address> No. 45, xx Street, xx City< / address> <tel> 010-83198897< / tel> <comment> Founded in 1959, Hospital C is a large-scale tertiary-level Class A general hospital that focuses on clinical and research work in neuroscience and geriatrics, specializes in the treatment of cardiovascular and cerebrovascular diseases, and undertakes medical, educational, scientific research, prevention, healthcare and rehabilitation tasks.< / comment> < / record> As can be seen from the query results above, each record is already an item in the XML file, and the exchanged text can be obtained simply by concatenating them.
[0024] Similarly, if you need to generate JSON format data, load the JSON file template into the SQL query language, embed the fields of the query results into specific locations in the JSON template, and after the SQL is executed, a record becomes an item in the JSON.
[0025] The query statement generated after loading the JSON template in SQL becomes: select concat(' { "ID": "',ID,'" \"name\":\"', name,'\", \"address\":\"', address,'\", "tel":"', tel,'", "comment": "',comment,'" }') from hospital order by ID asc; `concat` is a built-in string concatenation function in SQL. The string enclosed in single quotes is a fixed string, which is the decomposed JSON template. The string `(\")` is the escape character used when double quotes are used in the SQL string. The strings enclosed in the fixed strings are the database fields.
[0026] The query results of the above SQL are also a two-dimensional table with only one column in memory, and the final JSON file can be obtained by simply concatenating the results.
[0027] S102. Execute the SQL query statement, concatenate the query results of the SQL statement in order, and generate structured exchange text; The structured exchange text is a carrier for data exchange between systems and modules, and can be an exchange file in formats such as XML and JSON.
[0028] Optionally, the content text of each record in the query results can be concatenated sequentially, and an enclosing tag (e.g., result) can be added to the beginning of all the concatenated content to obtain structured exchange text.
[0029] Each record in the query results is an item in the structured exchange text. To generate the final structured text, the exchange text needs to be concatenated.
[0030] If the structured exchange text is in XML format, the content text of each generated record will be concatenated sequentially, with summary tags added at the beginning and end. <result> and< / result> This completes the generation of the entire XML file.
[0031] For example, a portion of the code is as follows: <result> <record> <id> 010001< / id> <name> Hospital A< / name> <address> No. 49, xx Road, xx City< / address> <tel> 010-82266698< / tel> <comment> Hospital A was founded in 1958 and is a comprehensive tertiary hospital integrating medical treatment, teaching, scientific research, preventive healthcare, rehabilitation and health management.< / comment> < / record> <record> <id> 010002< / id> <name> Hospital B< / name> <address> No. 8, xx Street, xx City< / address> <tel> 010-83572212< / tel> <comment> Hospital B, located in area B, is a large-scale comprehensive tertiary-level Class A hospital integrating medical treatment, teaching, scientific research, and prevention. It serves as a healthcare base hospital for area B.< / comment> < / record> <record> <id> 010003< / id> <name> C Hospital< / name> <address> No. 45, xx Street, xx City< / address> <tel> 010-83198897< / tel> <comment> Founded in 1959, Hospital C is a large-scale tertiary-level Class A general hospital that focuses on clinical and research work in neuroscience and geriatrics, specializes in the treatment of cardiovascular and cerebrovascular diseases, and undertakes medical, educational, scientific research, prevention, healthcare and rehabilitation tasks.< / comment> < / record> < / result> If the structured exchange text is in JSON format, the content text of each generated record will be concatenated sequentially, with commas added between them. The corresponding root key string (e.g., "result":) will be added at the beginning of the file, and the corresponding closing brackets will be added at the end of the file to complete the generation of the entire JSON file.
[0032] If the query result of the SQL statement contains structured text reserved words, an automatic replacement function is added to the corresponding field in the SQL query statement to replace the structured text reserved words with legal escape characters.
[0033] If the content of a database field may contain symbols that appear as reserved words in structured text, a symbol replacement function needs to be added to the relevant field portion of the SQL query statement. For example, the content stored in the comment field of the database may contain reserved characters from XML, such as greater than or less than signs. In this case, a replacement function needs to be added to this field to replace the reserved characters with legal escape characters in the XML content, such as replacing "<" with "<" and ">" with ">".
[0034] Optionally, the automatic replacement function is the SQL built-in replace function. replace is a built-in SQL function used for finding and replacing content.
[0035] In this embodiment, the string processing capabilities of the standardized Structured Query Language (SQL) are utilized to combine the template of the structured text with the data field to be queried, forming a query statement with a pre-defined structured text structure and information. After executing the database query, the query results are automatically embedded in the corresponding positions of the formatted text. Subsequently, only simple text concatenation of the query results is needed to form a complete structured text for data exchange. When data exchange is required, if different specifications of structured text formats are needed, different structured text templates can be loaded when generating the database query statement to form structured text of different specifications for data exchange.
[0036] Compared to existing methods for generating structured text for exchange, the solution provided in this implementation not only greatly simplifies the implementation process and reduces the amount of code required, but also boasts high execution efficiency and does not rely on third-party read / write libraries. By loading different templates, it can support various structured text formats, adapting to one-to-many data exchange between systems, significantly improving development and execution efficiency, and is easy to maintain. Furthermore, it is low-cost, simple, and practical to implement.
[0037] It should be understood that the sequence number of each step in the above embodiments does not imply the order of execution. The execution order of each process should be determined by its function and internal logic, and should not constitute any limitation on the implementation process of the embodiments of the present invention.
[0038] Figure 2 A schematic diagram of a structured text generation system provided in an embodiment of the present invention is shown. The system includes: The query statement construction module 210 is used to load the template of structured text and construct the corresponding database SQL query statement; This involves loading the structured text template into the SQL query language and embedding the pre-queried fields from the structured text template into the corresponding positions in the SQL query statement.
[0039] The exchange text generation module 220 is used to execute SQL query statements, concatenate the query results of the SQL statements in sequence, and generate structured exchange text. Optionally, the text content of each record in the query results can be concatenated sequentially, and an enclosing tag can be added to the beginning of all the concatenated text to obtain structured exchange text.
[0040] If the query result of the SQL statement contains structured text reserved words, an automatic replacement function is added to the corresponding field in the SQL query statement to replace the structured text reserved words with legal escape characters.
[0041] The automatic replacement function is the SQL-embedded replace function.
[0042] Those skilled in the art will understand that, for the sake of convenience and brevity, the specific working process of the system and modules described above can be referred to the corresponding process in the foregoing method embodiments, and will not be repeated here.
[0043] Figure 3 This is a schematic diagram of an electronic device according to an embodiment of the present invention. The electronic device is used to generate structured exchange text. Figure 3As shown, the electronic device 3 in this embodiment includes: a memory 310, a processor 320, and a system bus 330. The memory 310 includes an executable program 3101 stored thereon. As those skilled in the art will understand, Figure 3 The electronic device structure shown does not constitute a limitation on the electronic device and may include more or fewer components than shown, or combine certain components, or have different component arrangements.
[0044] The following is combined with Figure 3 A detailed introduction to each component of the electronic device: The memory 310 can be used to store software programs and modules. The processor 320 executes various functional applications and data processing of the electronic device by running the software programs and modules stored in the memory 310. The memory 310 may mainly include a program storage area and a data storage area. The program storage area may store the operating system, application programs required for at least one function (such as sound playback function, image playback function, etc.), etc.; the data storage area may store data created according to the use of the electronic device (such as cached data), etc. In addition, the memory 310 may include high-speed random access memory, and may also include non-volatile memory, such as at least one disk storage device, flash memory device, or other volatile solid-state storage device.
[0045] An executable program 3101 containing a data conversion method is stored in memory 310. This executable program 3101 can be divided into one or more modules / units, which are stored in memory 310 and executed by processor 320 to achieve structured exchange text generation, etc. The one or more modules / units can be a series of computer program instruction segments capable of performing specific functions, describing the execution process of the executable program 3101 in the electronic device 3. For example, the executable program 3101 can be divided into functional modules such as a query statement construction module and an exchange text generation module.
[0046] Processor 320 is the control center of the electronic device. It connects various parts of the electronic device via various interfaces and lines. By running or executing software programs and / or modules stored in memory 310, and by calling data stored in memory 310, it performs various functions and processes data, thereby monitoring the overall status of the electronic device. Optionally, processor 320 may include one or more processing units; preferably, processor 320 may integrate an application processor and a modem processor, wherein the application processor mainly handles the operating system, application programs, etc., and the modem processor mainly handles wireless communication. It is understood that the aforementioned modem processor may not be integrated into processor 420.
[0047] The system bus 330 is used to connect various functional components within the computer, transmitting data, address, and control information. Its type can be, for example, a PCI bus, an ISA bus, or a CAN bus. Instructions from the processor 320 are transmitted to the memory 310 via the bus, and the memory 310 sends data back to the processor 320. The system bus 330 is responsible for data and instruction exchange between the processor 320 and the memory 310. Of course, the system bus 330 can also connect to other devices, such as network interfaces and display devices.
[0048] In this embodiment of the invention, the executable program executed by the processor 320 included in the electronic device includes: Load the template of the structured text and construct the corresponding SQL query statement for the database; Execute SQL query statements, concatenate the query results in order, and generate structured exchange text; If the query result of the SQL statement contains structured text reserved words, an automatic replacement function is added to the corresponding field in the SQL query statement to replace the structured text reserved words with legal escape characters.
[0049] Those skilled in the art will understand that, for the sake of convenience and brevity, the specific working processes of the systems, devices, and modules described above can be referred to the corresponding processes in the foregoing method embodiments, and will not be repeated here.
[0050] In the above embodiments, the descriptions of each embodiment have different focuses. For parts that are not described in detail or recorded in a certain embodiment, please refer to the relevant descriptions of other embodiments.
[0051] The above-described embodiments are only used to illustrate the technical solutions of the present invention, and are not intended to limit it. Although the present invention has been described in detail with reference to the foregoing embodiments, those skilled in the art should understand that modifications can still be made to the technical solutions described in the foregoing embodiments, or equivalent substitutions can be made to some of the technical features. Such modifications or substitutions do not cause the essence of the corresponding technical solutions to deviate from the spirit and scope of the technical solutions of the embodiments of the present invention.
Claims
1. A method for generating structured exchange text, characterized in that, include: Load the template of the structured text and construct the corresponding SQL query statement for the database; Execute SQL query statements, concatenate the query results in order, and generate structured exchange text; If the query result of the SQL statement contains structured text reserved words, an automatic replacement function is added to the corresponding field in the SQL query statement to replace the structured text reserved words with legal escape characters.
2. The method according to claim 1, characterized in that, The process of loading the structured text template and constructing the corresponding database SQL query statement also includes: The structured text template is loaded into the SQL query language, and the fields to be queried in the structured text template are embedded into the corresponding positions in the SQL query statement.
3. The method according to claim 1, characterized in that, The process of sequentially concatenating the query results of the SQL statement to generate structured exchange text includes: The text content of each record in the query results is concatenated sequentially, and an enclosing tag is added to the beginning of all the concatenated content to obtain structured exchange text.
4. The method according to claim 1, characterized in that, The automatic replacement function is the SQL-embedded replace function.
5. A structured exchange text generation system, characterized in that, include: The query statement construction module is used to load structured text templates and construct corresponding database SQL query statements; The exchange text generation module is used to execute SQL query statements, concatenate the query results of the SQL statements in sequence, and generate structured exchange text. If the query result of the SQL statement contains structured text reserved words, an automatic replacement function is added to the corresponding field in the SQL query statement to replace the structured text reserved words with legal escape characters.
6. The system according to claim 5, characterized in that, The process of loading the structured text template and constructing the corresponding database SQL query statement also includes: The structured text template is loaded into the SQL query language, and the fields to be queried in the structured text template are embedded into the corresponding positions in the SQL query statement.
7. The system according to claim 5, characterized in that, The process of sequentially concatenating the query results of the SQL statement to generate structured exchange text includes: The text content of each record in the query results is concatenated sequentially, and an enclosing tag is added to the beginning of all the concatenated content to obtain structured exchange text.
8. The system according to claim 5, characterized in that, The automatic replacement function is the SQL-embedded replace function.
9. An electronic device comprising a memory, a processor, and a computer program stored in the memory and executable on the processor, characterized in that, When the processor executes the computer program, it implements the steps of a structured exchange text generation method as described in any one of claims 1 to 4.
10. A computer-readable storage medium storing a computer program, characterized in that, When the computer program is executed, it implements the steps of a structured exchange text generation method as described in any one of claims 1 to 4.