Efficient analysis method and system for directly mapping XML (Extensible Markup Language) data to C language structural body
By introducing reflection mechanism and modular design in the C language environment, an efficient parsing method for directly mapping XML data to C language structures is realized, solving the problems of cumbersome manual mapping and high maintenance costs in the existing technology, and improving development efficiency and code maintainability.
Patent Information
- Application Number
- CN202510222711.1
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2025-02-27
- Publication Date
- 2025-05-30
AI Technical Summary
When mapping XML data to C language structures, the manual mapping process is cumbersome and error-prone, and the maintenance cost is high, especially when dealing with complex XML structures.
By introducing reflection mechanism and modular design, it provides efficient parsing methods for XML data to be mapped directly to C language structures, including building a DOM tree or SAX event stream, initializing a reflection table, calling the main parsing function, traversing XML nodes, calling parsing functions, performing memory allocation and error processing.
It significantly simplifies the parsing and mapping process of XML data, reduces development and maintenance costs, improves development efficiency and code maintainability, optimizes memory management and error handling, and improves analysis performance and flexibility.
Smart Images

Figure CN120066588A_ABST
Abstract
Description
Technical Field
[0001] The present invention relates to the field of embedded technologies, and in particular to an efficient parsing method and system for directly mapping XML data to a C language structure. Background Art
[0002] In modern software development, the choice of data exchange format is crucial for interoperability between different systems. XML (Extensible Markup Language), as a widely accepted and platform-independent data representation, plays an important role in data exchange between heterogeneous systems. It is not only used for configuration files, Web service communication, but also commonly used for storing and transmitting structured data. C language, as one of the main programming languages in system programming and embedded development, although it is highly efficient in performance and has strong hardware control capabilities, it does not have the ability to directly process XML itself. In order to enable a C language program to easily read, parse, and generate XML documents, developers usually need to rely on third-party libraries or write their own parsers. Common C language XML parsing libraries include Expat, Libxml2, etc., which provide APIs to parse XML documents and build DOM trees or SAX event streams. However, these libraries often require developers to manually traverse nodes, extract attributes and content, and map them to the internal data structures of the application. Such a process is cumbersome and error-prone, especially when dealing with complex XML structures.
[0003] Many developers will choose to write customized XML parsing code for specific application scenarios. The advantage of this approach is that the parsing logic can be fully optimized according to requirements, avoiding unnecessary overhead. However, this also means that whenever the XML structure changes or a new data type is introduced, the parsing code needs to be updated, resulting in a relatively high maintenance cost.
[0004] Another common way is to use an existing XML library + manual mapping, that is, to combine an existing XML parsing library (such as Expat or Libxml2), and the developer is responsible for mapping the parsing results to a C language structure. Although this method can utilize the functions of a mature library, it still has the above-mentioned manual mapping problem, increasing the development and maintenance workload.
[0005] There are also some tools that can automatically generate C language structures and corresponding parsing code at compile time according to an XML schema (Schema). This approach can improve development efficiency to a certain extent, but it also brings additional complexity and dependencies. In addition, the generated code may not be as flexible or optimized as handwritten code. Summary of the Invention
[0006] The object of the present invention is to provide an efficient parsing method and system for directly mapping XML data to a C language structure. On the basis of maintaining the high efficiency of the C language, by introducing a reflection mechanism and modular design, the parsing and mapping process of XML data is significantly simplified, the development and maintenance costs are reduced, and it has high practical value.
[0007] To achieve the above object, the present invention provides an efficient parsing method for directly mapping XML data to a C language structure, including the following steps:
[0008] S1. Input the XML document, take the XML document provided by the user as the input, and construct a DOM tree or a SAX event stream;
[0009] S2. Initialize the reflection table, initialize the reflection table according to the definition of the target structure, and describe the mapping relationship between XML nodes and structure fields;
[0010] S3. Call the main parsing function: call the _XmlObj2Struct function to start the parsing process;
[0011] S4. Traverse the XML nodes: the main parsing function _XmlObj2Struct traverses the XML nodes and calls the corresponding parsing functions according to the information in the reflection table;
[0012] S5. Call the parsing function: call the corresponding static parsing function in the parsing function library according to the node type;
[0013] S6. Memory allocation: for data with dynamic size, call the struct_malloc function for memory allocation to ensure memory alignment and avoid leakage;
[0014] S7. Fill the structure: the parsing function fills the parsing result into the target structure;
[0015] S8. Error handling: for errors encountered during the parsing process, record the error information through X2T_LOG, encapsulate the error information as an error code and return it;
[0016] S9. Return the result: after the parsing is completed, return the filled structure or error information.
[0017] Preferably, the reflection table supports multiple data types, including strings, integers, floating-point numbers, arrays, and nested objects, and allows support for new data types to be added by extending the reflection table.
[0018] Preferably, in S5, if the current field is of string type, call the parseXmlString function for parsing; if the current field is of 32-bit integer type, call the parseXmlInt32 function for parsing; if the current field is of floating-point type, call the parseXmlFloat function for parsing.
[0019] Preferably, in S5, if the current field is of array type, call the parseXmlArray function to automatically calculate the length of the array, store it in a preposed int32_t variable, and then recursively call the _XmlObj2Struct function for parsing.
[0020] Preferably, in S5, if the current field is a nested object, recursively call the _XmlObj2Struct function and the parseXmlObject function to parse each child node layer by layer.
[0021] An efficient parsing system for directly mapping XML data to a C language structure, including:
[0022] An XML parser for parsing the input XML document and constructing a DOM tree or a SAX event stream;
[0023] A reflection table module for defining and storing the mapping relationship between XML nodes and C language structure fields;
[0024] A parsing function library module containing multiple static parsing functions, and each static parsing function is responsible for parsing XML nodes of a specific type;
[0025] A dynamic memory management module for dynamically allocating and managing memory to ensure memory alignment and avoid leaks;
[0026] An error handling and logging module for recording error information during the parsing process;
[0027] An external interface module providing a concise API for developers to call to complete the conversion from XML to a structure.
[0028] Preferably, the external interface module includes the _XmlObj2Struct function and the getStructBufSize function. The _XmlObj2Struct function is used to parse and populate the target structure with XML nodes, and the getStructBufSize function is used to estimate the memory size required for parsing to ensure the accuracy of memory allocation.
[0029] A computer-readable storage medium stores a computer program, and when the computer program is executed by a processor, it implements the efficient parsing method for directly mapping the XML data to a C language structure.
[0030] According to the specific embodiments provided by the present invention, the following technical effects are disclosed by the present invention:
[0031] (1) Improve development efficiency: Through automated mapping and modular design, the present invention significantly reduces the workload of manually writing parsing code, enabling developers to implement functions faster.
[0032] (2) Enhance code maintainability: The reflection table mechanism and modular design make the code more concise, readable, and maintainable, reducing the maintenance cost, especially when dealing with complex or frequently changing XML structures.
[0033] (3) Optimize memory management: The dynamic memory allocation and on-demand allocation mechanism ensure the efficiency and security of memory usage, which is particularly suitable for resource-constrained environments or scenarios of processing large-scale XML data.
[0034] (4) Unified error handling and logging: The systematic error handling mechanism and detailed logging function enhance the robustness and debuggability of the system, helping developers to discover and fix problems faster.
[0035] (5) Improve parsing performance: Modular design, recursive parsing mechanism, and on-demand loading of data significantly improve the parsing speed and efficiency, which is particularly suitable for processing large-scale or complex XML data.
[0036] (6) Enhance flexibility and scalability: The scalability of the reflection table and the modular parsing function library enable the system to quickly adapt to changes in business requirements, with good flexibility and scalability.
[0037] The technical solutions of the present invention will be further described in detail below through the accompanying drawings and embodiments. Description of the Drawings
[0038] In order to more clearly illustrate the technical solutions in the embodiments of the present invention or the prior art, the drawings required for use in the embodiments will be briefly introduced below. Obviously, the drawings in the following description are only some embodiments of the present invention, and those of ordinary skill in the art can also obtain other drawings based on these drawings without creative efforts.
[0039] Figure 1 Schematic diagram of the efficient parsing method for directly mapping XML data to a C language structure in an embodiment of the present invention;
[0040] Figure 2 Schematic diagram of the structure of the efficient parsing system for directly mapping XML data to a C language structure in an embodiment of the present invention;
[0041] Figure 3Block diagram of the reflection table according to an embodiment of the present invention;
[0042] Figure 4 Timing diagram of the dynamic memory management module according to an embodiment of the present invention;
[0043] Figure 5 Timing diagram of the parsing function library according to an embodiment of the present invention;
[0044] Figure 6 Flowchart of the error handling and logging module according to an embodiment of the present invention. Detailed implementation manners
[0045] Next, the technical solutions in the embodiments of the present invention will be clearly and completely described in conjunction with the accompanying drawings in the embodiments of the present invention. Obviously, the described embodiments are only a part of the embodiments of the present invention, rather than all the embodiments. All other embodiments obtained by those of ordinary skill in the art based on the embodiments of the present invention without creative efforts shall fall within the protection scope of the present invention.
[0046] To make the above objects, features, and advantages of the present invention more obvious and understandable, the present invention will be further described in detail below with reference to the accompanying drawings and specific implementation manners.
[0047] Embodiment
[0048] An efficient parsing system for directly mapping XML data to C language structures, as Figure 2 shown, includes:
[0049] An XML parser for parsing the input XML document and constructing a DOM tree or SAX event stream;
[0050] A reflection table module for defining and storing the mapping relationship between XML nodes and C language structure fields;
[0051] A parsing function library module containing multiple static parsing functions, and each static parsing function is responsible for parsing a specific type of XML node;
[0052] A dynamic memory management module for dynamically allocating and managing memory to ensure memory alignment and avoid leakage;
[0053] An error handling and logging module for recording error information during the parsing process;
[0054] The external interface module provides a simple API for developers to call to complete the conversion from XML to a structure. The external interface module includes the _XmlObj2Struct function and the getStructBufSize function. The _XmlObj2Struct function is used to parse XML nodes and populate them into the target structure, and the getStructBufSize function is used to estimate the memory size required for parsing to ensure the accuracy of memory allocation. The present invention provides two main external interface functions, XmlObj2Struct and getStructBufSize, enabling users to complete the conversion from XML to a structure without caring about the underlying details. Just by calling these two functions. This simple API design lowers the usage threshold and enhances the user experience.
[0055] As Figure 1 shown, an efficient parsing method for directly mapping XML data to a C language structure includes the following steps:
[0056] S1. Input the XML document, take the XML document provided by the user as the input, and construct a DOM tree or a SAX event stream. XML (eXtensible Markup Language) is a markup language used for storing and transmitting data. It is similar to HTML, but XML is mainly used for structured data rather than displaying data.
[0057] S2. Initialize the reflection table, initialize the reflection table (reflect_item_t) according to the definition of the target structure, and describe the mapping relationship between XML nodes and structure fields. As Figure 3 shown, the reflection table contains information such as the name, offset, size, and type of each field.
[0058] Field name: Represents the name of the XML node or the attribute key.
[0059] Offset: Represents the offset of this field in the target structure.
[0060] Size: Represents the size of this field in memory.
[0061] Type: Represents the data type of this field (such as XML_STRING, XML_INT32, XML_ARRAY, etc.).
[0062] Sub-reflection table: For complex structures (such as objects, arrays), a sub-reflection table can be recursively defined to describe the mapping relationship of its internal fields.
[0063] End flag: Used to identify the end position of the reflection table.
[0064] The reflection table supports multiple data types, including strings, integers, floating-point numbers, arrays, and nested objects, and allows support for new data types to be added by extending the reflection table. By defining the reflection table (reflect_item_t), the mapping relationship between XML nodes and C language structure fields is described. This design makes the parsing logic clearer and is also convenient for extension and maintenance. At the same time, the XML nodes are automatically mapped to the C language structure fields, reducing the workload of developers writing parsing code manually.
[0065] S3. Call the main parsing function: Call the _XmlObj2Struct function to start the parsing process;
[0066] S4. Traverse XML nodes: The main parsing function _XmlObj2Struct traverses XML nodes and calls the corresponding parsing functions according to the information in the reflection table;
[0067] S5. Call the parsing function: Call the corresponding static parsing function in the parsing function library according to the node type; as Figure 5 shown, if the current field is of string type, call the parseXmlString function for parsing; if the current field is of 32-bit integer type, call the parseXmlInt32 function for parsing; if the current field is of floating-point type, call the parseXmlFloat function for parsing.
[0068] If the current field is of array type, call the parseXmlArray function to automatically calculate the length of the array, store it in a prefixed int32_t variable, and then recursively call the _XmlObj2Struct function for parsing. If the current field is a nested object, recursively call the _XmlObj2Struct function and the parseXmlObject function to parse each sub-node layer by layer.
[0069] Functions for parsing strings, integers, floating-point numbers, arrays, and nested objects, etc. already exist in existing XML parsing libraries, but these functions are usually scattered and require developers to write a large amount of code manually to implement mapping and memory management. The present invention adopts a modular parsing function library, where each function is responsible for parsing XML nodes of a specific type, achieving automatic mapping and efficient parsing, and reducing the workload. This design makes the code structure clear and easy to understand and maintain. When new data types need to be added or existing types need to be modified, only the corresponding parsing functions need to be modified without affecting other parts.
[0070] S6. Memory allocation: For data with dynamic sizes (such as strings, arrays), call the struct_malloc function for memory allocation to ensure memory alignment and avoid leaks.
[0071] The timing of the dynamic memory management module that performs memory allocation is as Figure 4 shown and has the following entries:
[0072] Buffer: A pre-allocated continuous memory segment used to store parsing results. It provides a fixed memory space, avoiding frequent memory allocation and deallocation operations and improving parsing efficiency.
[0073] Index: Records the currently used memory size to ensure that each allocation does not exceed the buffer range and prevent memory overflow.
[0074] 8-byte alignment: Each time memory is allocated, ensure that the allocated memory block is 8-byte aligned to meet the alignment requirements of the C language, improve memory access efficiency, and avoid performance issues or access conflicts caused by improper memory alignment.
[0075] Memory allocation function: The struct_malloc function allocates memory from the buffer according to the required size and alignment requirements and updates the index. It provides dynamic memory allocation functionality, ensuring memory alignment and avoiding memory overflow.
[0076] Memory release: When parsing is complete, the entire buffer can be released to avoid memory leaks and ensure effective management of system resources.
[0077] S7. Populate the structure: The parsing function populates the parsing results into the target structure;
[0078] S8. Error handling: For errors encountered during parsing, log the error information through X2T_LOG, encapsulate the error information into an error code, and return it; the process is as Figure 6 shown.
[0079] Detect errors: In each parsing step, check for the existence of errors (such as format errors, memory shortages, etc.).
[0080] Log error information: If an error is detected, record detailed error information through the X2T_LOG function, including the error type, occurrence location, and relevant context.
[0081] Return error code: Encapsulate the error information into an error code and return it to the caller.
[0082] Restore state: In some cases, it may be necessary to restore the system state to avoid errors affecting subsequent operations.
[0083] Continue execution: If the error is ignorable, you can choose to continue the parsing process or terminate the parsing prematurely.
[0084] The present invention conducts unified error logging through X2T_LOG, which helps developers quickly locate and solve problems. This systematic error handling mechanism makes debugging more convenient and improves the robustness of the system. In addition, detailed error information and debugging clues are provided, enabling developers to more easily trace and fix problems.
[0085] S9. Return result: After parsing is completed, a filled structure or error information is returned.
[0086] In summary, although C language itself does not have a built-in reflection mechanism, the present invention realizes an automatic mapping function similar to high-level languages such as Java and C# by defining a reflection table and corresponding parsing functions, making the conversion between XML and structures more intuitive and efficient. The present invention not only supports basic data types (such as integers, floating-point numbers, strings, etc.), but also supports complex structures (such as objects, arrays, etc.), and can easily add support for new data types by extending the reflection table. By introducing a reflection mechanism, modular design, and dynamic memory management, the present invention significantly simplifies the process of XML data parsing and mapping in a C language environment, and solves problems existing in the prior art such as complex manual mapping, difficult memory management, and inconsistent error handling. It not only improves development efficiency and code maintainability, but also performs excellently in terms of performance and stability, and has broad application prospects.
[0087] A computer-readable storage medium stores a computer program, and when the computer program is executed by a processor, it implements the efficient parsing method for directly mapping XML data to a C language structure as described above.
[0088] For the remaining technical features in the above embodiments, those skilled in the art can flexibly select them according to actual situations to meet different specific actual needs. However, it is obvious to those of ordinary skill in the art that these specific details do not have to be adopted to implement the present invention. In other instances, in order to avoid confusing the present invention, well-known components, structures, or parts are not specifically described, and all are within the scope of the technical solutions claimed in the claims of the present invention.
[0089] Modifications and changes made by those skilled in the art that do not depart from the spirit and scope of the present invention shall fall within the protection scope of the appended claims of the present invention. In the above description, in order to provide a thorough understanding of the present invention, a large number of specific details are elaborated. However, it is obvious to those of ordinary skill in the art that these specific details do not have to be adopted to implement the present invention. In other instances, in order to avoid confusing the present invention, well-known technologies are not specifically described, such as specific construction details, working conditions, and other technical conditions.
[0090] In this article, specific examples are used to elaborate on the principles and implementation manners of the present invention. The description of the above embodiments is only used to help understand the method and its core idea of the present invention; at the same time, for those of ordinary skill in the art, according to the idea of the present invention, there will be changes in the specific implementation manners and application scopes. In summary, the content of this specification should not be construed as a limitation to the present invention.
Claims
1. An efficient parsing method for directly mapping XML data to C language structures, characterized in that: The following steps are involved: S1. Input XML document: Take the XML document provided by the user as input and build a DOM tree or SAX event stream; S2. Initialize the reflection table according to the definition of the target structure, and describe the mapping relationship between the XML node and the structure field; S3. Call the main parsing function: call the _XmlObj2Struct function to start the parsing process; S4. Traversing XML nodes: The main parsing function _XmlObj2Struct traverses the XML nodes and calls the corresponding parsing function according to the information in the reflection table; S5. Calling parsing function: calling the corresponding static parsing function in the parsing function library according to the node type; S6. Memory allocation: For data of dynamic size, call the struct_malloc function to allocate memory to ensure memory alignment and avoid leaks; S7, fill the structure: the parsing function fills the parsing results into the target structure; S8. Error handling: For errors encountered during the parsing process, the error information is recorded through X2T_LOG, and the error information is encapsulated as an error code and returned; S9. Return result: After parsing is completed, a filled structure or error information is returned.
2. The efficient parsing method for directly mapping XML data to C language structures according to claim 1, characterized in that: The reflection table supports a variety of data types, including strings, integers, floating point numbers, arrays, and nested objects, and allows support for new data types to be added by extending the reflection table.
3. The efficient parsing method for directly mapping XML data to C language structures according to claim 2, characterized in that: In S5, if the current field is a string type, the parseXmlString function is called for parsing; if the current field is a 32-bit integer type, the parseXmlInt32 function is called for parsing; if the current field is a floating point type, the parseXmlFloat function is called for parsing.
4. The efficient parsing method for directly mapping XML data to C language structures according to claim 2, characterized in that: In S5, if the current field is of array type, the parseXmlArray function is called to automatically calculate the length of the array, store it in a prepended int32_t variable, and then the _XmlObj2Struct function is recursively called for parsing.
5. The efficient parsing method for directly mapping XML data to C language structures according to claim 2, characterized in that: In S5, if the current field is a nested object, the _XmlObj2Str uct function and the parseXmlObject function are recursively called to parse each child node layer by layer.
6. An efficient parsing system that directly maps XML data to C language structures, characterized by: include: XML parser, used to parse the input XML document and build a DOM tree or SAX event stream; The reflection table module is used to define and store the mapping relationship between XML nodes and C language structure fields; Parsing function library module, including multiple static parsing functions, each of which is responsible for parsing a specific type of XML node; Dynamic memory management module, used to dynamically allocate and manage memory, ensure memory alignment and avoid leaks; Error handling and logging module, used to record error information during parsing; The external interface module provides a simple API for developers to call to complete the conversion from XML to structure.
7. The efficient parsing system for directly mapping XML data to C language structures according to claim 6, characterized in that: The external interface module includes the _XmlObj2Struct function and the getStructBufSize function. The _XmlObj2Struct function is used to parse the XML nodes and fill them into the target structure. The getStructBufSize function is used to estimate the memory size required for parsing to ensure the accuracy of memory allocation.
8. A computer-readable storage medium, characterized in that: A computer program is stored, and when the computer program is executed by a processor, an efficient parsing method for directly mapping XML data to a C language structure according to any one of claims 1 to 6 is implemented.