Xml file analysis method and system and storage medium
By constructing a node index mapping file for XML files, the memory consumption problem during XML file parsing in existing technologies is solved, achieving efficient local access and memory optimization.
Patent Information
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- 航天银山电气有限公司
- Filing Date
- 2025-12-19
- Publication Date
- 2026-05-05
AI Technical Summary
Existing XML file parsing methods still require scanning the entire file from beginning to end when the parsing requirement is only for a single node, resulting in high system memory consumption and affecting resource utilization.
By scanning the target XML file, reading the node index information of each node, constructing a mapping file, and retrieving the target node data from the mapping file when receiving a parsing request, memory usage is reduced.
A mapping file is generated during the preprocessing stage, allowing for subsequent local access to the original XML file. This reduces system memory usage and improves parsing efficiency and resource utilization.
Smart Images

Figure CN121981106A_ABST
Abstract
Description
Technical Field
[0001] This application relates to, but is not limited to, the field of data processing technology, and in particular to an XML file parsing method, system, and storage medium. Background Technology
[0002] Existing methods for parsing and reading XML files include DOM parsing, SAX parsing, and JAXB parsing. When parsing XML files based on these methods, even if the parsing requirement is only for a single node in the XML file, the entire XML file must be scanned from beginning to end, which will consume a large amount of system memory. When there are many XML files to be parsed at the same time, it will affect the utilization of system resources. Summary of the Invention
[0003] This application provides an XML file parsing method, system, and storage medium, which can effectively reduce the system memory usage during XML file parsing.
[0004] In a first aspect, embodiments of this application provide a DOM-based XML file parsing method, including: The target XML file is scanned, and the node index information of each node in the target XML file is read. A mapping file is constructed based on all the node index information, and the mapping file is associated with the target XML file. The node index information of any node includes the node identifier of the corresponding node, the position offset of the node identifier in the target XML file, and the node content length of the node. The memory usage of the mapping file is less than the memory usage of the target XML file. A parsing request for the target XML file is received, the parsing request including the target node; Obtain the target node identifier, target offset, and target content length corresponding to the target node from the mapping file; Target node data is obtained from the target XML file based on the target node identifier, target offset, and target content length. The target node data is parsed to obtain the node attribute information of the target node.
[0005] In some embodiments, scanning a target XML file and reading the node index information of each node in the target XML file includes: Load the target XML file into memory; When traversing the target XML file in memory, if the node start character of any node is detected, record the node identifier corresponding to any node, the position offset of the node identifier in the corresponding target XML file, and the node content length of the node. The node identifier, the position offset, and the node content length are determined as the node index information corresponding to the node.
[0006] In some embodiments, after recording the node identifier corresponding to any of the nodes, the position offset of the node identifier in the target XML file, and the node content length of the node, the method further includes: Record the pointer to the currently visited node; When the pointer is greater than the character length of the target XML file in the memory, the node index information of each node in the target XML file is read. If the pointer is less than the character length, continue to detect the node start character.
[0007] In some embodiments, obtaining the target node identifier, target offset, and target content length corresponding to the target node from the mapping file includes: The DOM tree is constructed from the mapped file using a DOM parser; Obtain the target node identifier, target offset, and target content length corresponding to the target node in the DOM tree.
[0008] Secondly, embodiments of this application provide an XML file parsing system, including: The mapping file generation module is used to scan the target XML file, read the node index information of each node in the target XML file, construct a mapping file based on all the node index information, and associate the mapping file with the target XML file. The node index information of any node includes the node identifier of the corresponding node, the position offset of the node identifier in the target XML file, and the node content length of the node. A parsing request receiving module is configured to receive a parsing request for the target XML file, wherein the parsing request includes a target node; The first data acquisition module is used to obtain the target node identifier, target offset and target content length corresponding to the target node from the mapping file; The second data acquisition module is used to acquire target node data from the target XML file based on the target node identifier, target offset, and target content length. The XML file parsing module is used to parse the target node data to obtain the node attribute information of the target node.
[0009] In some embodiments, the mapping file generation module includes: The first file loading module is used to load the target XML file into memory; The file scanning module is used to traverse the target XML file in the memory. When the node start character of any node is detected, the node identifier corresponding to any node, the position offset of the node identifier in the corresponding target XML file, and the node content length of the node are recorded. The node index information generation module is used to determine the node identifier, the position offset, and the node content length as the node index information of the corresponding node.
[0010] In some embodiments, the mapping file generation module further includes: The third data acquisition module is used to record the pointers of the currently traversed nodes; The first data processing module is used to read the node index information of each node in the target XML file when the pointer is greater than the character length of the target XML file in the memory; The second data processing module is used to continue detecting the node start character when the pointer is less than the character length.
[0011] In some embodiments, the first data acquisition module includes: The third data processing module is used to construct a DOM tree from the mapping file using a DOM parser; The fourth data acquisition module is used to obtain the target node identifier, target offset, and target content length corresponding to the target node in the DOM tree.
[0012] Secondly, embodiments of this application provide a control device, including at least one control processor and a memory for communicatively connecting to the at least one control processor; the memory stores instructions executable by the at least one control processor, which, when executed by the at least one control processor, enable the at least one control processor to perform the XML file parsing method as described in the first aspect.
[0013] Thirdly, embodiments of this application also provide an electronic device, including the control device of the second aspect.
[0014] Fourthly, embodiments of this application also provide a computer-readable storage medium storing computer-executable instructions for performing the XML file parsing method as described in the first aspect.
[0015] This application provides an XML file parsing method, system, and storage medium. The method includes: scanning a target XML file, reading the node index information of each node in the target XML file, constructing a mapping file based on all the node index information, and associating the mapping file with the target XML file. Each node index information includes the node identifier of the corresponding node, the position offset of the node identifier in the target XML file, and the node content length of the node. The memory usage of the mapping file is less than the memory usage of the target XML file. The method also includes receiving a parsing request for the target XML file, the parsing request including a target node; obtaining the target node identifier, target offset, and target content length corresponding to the target node from the mapping file; obtaining target node data from the target XML file based on the target node identifier, target offset, and target content length; and parsing the target node data to obtain the node attribute information of the target node. According to the solution provided in the embodiments of this application, a mapping file for XML files is generated during the preprocessing stage. Subsequently, each time an XML parsing request is received, only the mapping file needs to be loaded to achieve partial access to the content that needs to be parsed in the original XML. Compared with the existing solution of parsing the entire original XML file, this effectively reduces the system memory usage during the XML file parsing process. Attached Figure Description
[0016] Figure 1 This is a flowchart of the steps of an XML file parsing method provided in one embodiment of this application; Figure 2 This is a schematic diagram of a module of an XML file parsing system provided in another embodiment of this application; Figure 3 This is a structural diagram of an XML file parsing system provided in another embodiment of this application. Detailed Implementation
[0017] To make the objectives, technical solutions, and advantages of this application clearer, the following detailed description is provided in conjunction with the accompanying drawings and embodiments. It should be understood that the specific embodiments described herein are merely illustrative and not intended to limit the scope of this application.
[0018] It is understandable that although functional modules are divided in the device schematic diagram and a logical order is shown in the flowchart, in some cases, the steps shown or described may be performed in a different order than the module division in the device or the order in the flowchart. The terms "first," "second," etc., in the specification, claims, or the aforementioned drawings are used to distinguish similar objects and are not necessarily used to describe a specific order or sequence.
[0019] Existing methods for parsing and reading XML files include DOM parsing, SAX parsing, and JAXB parsing. When parsing XML files based on these methods, even if the parsing requirement is only for a single node in the XML file, the entire XML file must be scanned from beginning to end, which will consume a large amount of system memory. When there are many XML files to be parsed at the same time, it will affect the utilization of system resources.
[0020] To address the aforementioned problems, this application provides an XML file parsing method, system, and storage medium. The method includes: scanning a target XML file, reading the node index information of each node in the target XML file, constructing a mapping file based on all the node index information, and associating the mapping file with the target XML file. Each node index information includes the node identifier of the corresponding node, the offset of the node identifier in the target XML file, and the node content length of the node. The memory usage of the mapping file is less than the memory usage of the target XML file. The method also includes receiving a parsing request for the target XML file, the parsing request including a target node; obtaining the target node identifier, target offset, and target content length corresponding to the target node from the mapping file; obtaining target node data from the target XML file based on the target node identifier, target offset, and target content length; and parsing the target node data to obtain the node attribute information of the target node. According to the solution provided in the embodiments of this application, a mapping file for XML files is generated during the preprocessing stage. Subsequently, each time an XML parsing request is received, only the mapping file needs to be loaded to achieve partial access to the content that needs to be parsed in the original XML. Compared with the existing solution of parsing the entire original XML file, this effectively reduces the system memory usage during the XML file parsing process.
[0021] The embodiments of this application will be further described below with reference to the accompanying drawings.
[0022] refer to Figure 1 , Figure 1 This is a flowchart illustrating the steps of an XML file parsing method according to an embodiment of this application. This application provides an XML file parsing method, which includes, but is not limited to, the following steps: Step S10: Scan the target XML file, read the node index information of each node in the target XML file, construct a mapping file based on all the node index information, and associate the mapping file with the target XML file. The node index information of any node includes the node identifier of the corresponding node, the position offset of the node identifier in the target XML file, and the node content length of the node. The memory usage of the mapping file is less than the memory usage of the target XML.
[0023] It is understandable that, before reading, writing and parsing the target XML file, this embodiment first loads the entire target XML file into memory, then traverses the contents of the target XML file and reads the node index information of each node in the target XML file. The node index information includes the node identifier of the corresponding node, the position offset of the node identifier in the target XML file and the length of the node content, thereby providing an effective data foundation for the subsequent construction of the mapping file.
[0024] It is understood that in this embodiment, after constructing a mapping file associated with the target XML file based on all node index information, the mapping file can facilitate each subsequent parsing request. The mapping file can quickly query the content of the corresponding node by the node name (the node name in this embodiment is the node identifier), and the memory footprint of the mapping file is smaller than that of the original target XML file. This provides effective support for reducing parsing time and memory footprint for subsequent on-demand XML file parsing operations.
[0025] Specifically, Figure 1 Step S10 involves scanning the target XML file and reading the node index information of each node in the target XML file, including but not limited to the following steps: Step S11: Load the target XML file into memory; Step S12: Traverse the target XML file in memory. When the node start symbol of any node is detected, record the node identifier corresponding to any node, the position offset of the node identifier in the corresponding target XML file, and the length of the node content. Step S13: Determine the node identifier, position offset, and node content length as the node index information of the corresponding node.
[0026] In addition, in some embodiments, after performing step S12, the XML file parsing method of this embodiment also includes, but is not limited to, the following steps: Step S121, after recording the node identifier corresponding to any node, the offset of the node identifier in the target XML file, and the length of the node content, the method further includes: Step S123: Record the pointer to the currently traversed node; Step S124: When the pointer is greater than the character length of the target XML file in memory, complete the reading operation of the node index information of each node in the target XML file; Step S125: When the pointer is less than the character length, continue to detect the node start character.
[0027] Understandably, the process begins by scanning the target XML file from the pointer position corresponding to the node start character of any node. It sequentially reads the node index information corresponding to each node in the target XML file and records the pointer currently being traversed. This continues until the current pointer is greater than the character length of the target XML file, indicating that the memory has been traversed completely, thus completing the traversal of the target XML file and the reading of all node index information. If the pointer is less than the character length, the process continues to check the node start character, indicating that the traversal is not yet complete, and a new node start character is checked.
[0028] Specifically, in this embodiment of the application, the node start symbol is '<'.
[0029] Specifically, in this embodiment, a mapping file map.xml is constructed based on all node index information. The node index information may also include the node length and the position offset of the node content. <name ap=""”al="”" tp=""”" tl=""”">The format is stored in the mapping file map.xml, where name is the node identifier, ap is the position offset of the starting character of the node identifier in the target XML file, al is the node length, tp is the position offset of the node content, and tl is the length of the node content.
[0030] Step S20: A parsing request for the target XML file is received. The parsing request includes the target node.
[0031] Specifically, the parsing request in this embodiment is determined by the user. The target node can be any node in the target XML file, or it can include all nodes in the target XML file.
[0032] Step S30: Obtain the target node identifier, target offset, and target content length corresponding to the target node from the mapping file.
[0033] Specifically, Figure 1 Step S30 includes, but is not limited to, the following steps: Step S31: Use the DOM parser to construct the DOM tree from the mapping file; Step S32: Obtain the target node identifier, target offset, and target content length corresponding to the target node in the DOM tree.
[0034] It is understood that in this embodiment, the mapping file has a small memory footprint, serving as a lightweight index file. Furthermore, the DOM supports random access, resulting in high query efficiency, making it suitable for frequent searches. This significantly reduces the XML file parsing time and memory usage during the parsing process. Moreover, referring to the description of the above embodiment, the mapping file is constructed based on the necessary information of each node in the original target XML file. Therefore, the DOM tree constructed based on this mapping file is also a lightweight tree structure. Compared to existing schemes that construct and store DOM trees with complete XML document structures, this greatly shortens the tree construction time, thereby effectively reducing the time spent parsing XML files and improving the user experience.
[0035] Step S40: Obtain target node data from the target XML file based on the target node identifier, target offset, and target content length; Step S50: Parse the target node data to obtain the node attribute information of the target node.
[0036] Understandably, after obtaining the target node identifier, target offset, and target content length related to the target node corresponding to the parsing request, the original target XML file is opened, and the original node data (i.e., target node data) is read. Then, the target node data is parsed a second time to obtain the node attribute information of the target node. Compared with the existing solution, the XML parsing operation based on user needs can be completed in a shorter time and with less memory usage.
[0037] like Figure 2 As shown, this application also provides an XML file parsing system 200, including: The mapping file generation module 210 is used to scan the target XML file, read the node index information of each node in the target XML file, construct a mapping file based on all the node index information, and associate the mapping file with the target XML file. The node index information of any node includes the node identifier of the corresponding node, the position offset of the node identifier in the target XML file, and the node content length of the node. The memory usage of the mapping file is less than the memory usage of the target XML. The parsing request receiving module 220 is used to receive parsing requests for the target XML file, and the parsing request includes the target node; The first data acquisition module 230 is used to obtain the target node identifier, target offset and target content length corresponding to the target node from the mapping file; The second data acquisition module 240 is used to acquire target node data from the target XML file based on the target node identifier, target offset and target content length; The XML file parsing module 250 is used to parse the target node data and obtain the node attribute information of the target node.
[0038] In some embodiments, the mapping file generation module 210 includes: The first file loading module 211 is used to load the target XML file into memory; The file scanning module 212 is used to traverse the target XML file in memory. When the node start character of any node is detected, it records the node identifier corresponding to any node, the position offset of the node identifier in the corresponding target XML file, and the node content length of the node. The node index information generation module 213 is used to determine the node identifier, position offset and node content length as the node index information of the corresponding node.
[0039] In some embodiments, the mapping file generation module 210 further includes: The third data acquisition module 214 is used to record the pointers of the currently traversed nodes; The first data processing module 215 is used to read the node index information of each node in the target XML file when the pointer is greater than the character length of the target XML file in memory; The second data processing module 216 is used to continue detecting the node start character when the pointer is less than the character length.
[0040] In some embodiments, the first data acquisition module 230 includes: The third data processing module 231 is used to construct a DOM tree from the mapping file using the DOM parser; The fourth data acquisition module 232 is used to obtain the target node identifier, target offset and target content length corresponding to the target node in the DOM tree.
[0041] It should be noted that the specific implementation of the XML file parsing system is basically the same as the specific implementation of the XML file parsing method described above, and will not be repeated here.
[0042] like Figure 3 As shown, Figure 3 This is a structural diagram of a control device provided in one embodiment of this application. The present invention also provides a control device 300, comprising: The processor 310 can be implemented using a general-purpose central processing unit (CPU), microprocessor, application specific integrated circuit (ASIC), or one or more integrated circuits, and is used to execute relevant programs to implement the technical solutions provided in the embodiments of this application. The memory 320 can be implemented as a read-only memory (ROM), static storage device, dynamic storage device, or random access memory (RAM). The memory 320 can store the operating system and other applications. When the technical solutions provided in the embodiments of this specification are implemented through software or firmware, the relevant program code is stored in the memory 320 and is called and executed by the processor 310 using the XML file parsing method of the embodiments of this application. Input / output interface 330 is used to realize information input and output; The communication interface 340 is used to enable communication and interaction between this device and other devices. Communication can be achieved through wired means (such as USB, network cable, etc.) or wireless means (such as mobile network, WIFI, Bluetooth, etc.). Bus 350 transmits information between various components of the device (e.g., processor 310, memory 320, input / output interface 330, and communication interface 340); The processor 310, memory 320, input / output interface 330 and communication interface 340 are connected to each other within the device via bus 350.
[0043] In addition, this application also provides an XML file parsing system, including the control device 300 described in the above embodiments.
[0044] In addition, this application embodiment also provides a storage medium, which is a computer-readable storage medium, storing a computer program that, when executed by a processor, implements the above-described XML file parsing method.
[0045] Memory, as a non-transitory computer-readable storage medium, can be used to store non-transitory software programs and non-transitory computer-executable programs. Furthermore, memory may include high-speed random access memory, and may also include non-transitory memory, such as at least one disk storage device, flash memory device, or other non-transitory solid-state storage device. In some embodiments, memory may optionally include memory remotely located relative to the processor, and these remote memories can be connected to the processor via a network. Examples of such networks include, but are not limited to, the Internet, intranets, local area networks, mobile communication networks, and combinations thereof. The device embodiments described above are merely illustrative, and the units described as separate components may or may not be physically separate, and may be located in one place or distributed across multiple network units. Some or all of the modules can be selected to achieve the purpose of this embodiment according to actual needs.
[0046] It will be understood by those skilled in the art that all or some of the steps and systems in the methods disclosed above can be implemented as software, firmware, hardware, and suitable combinations thereof. Some or all of the physical components can be implemented as software executed by a processor, such as a central processing unit, digital signal processor, or microprocessor, or as hardware, or as an integrated circuit, such as an application-specific integrated circuit. Such software can be distributed on a computer-readable medium, which can include computer storage media (or non-transitory media) and communication media (or transient media). As is known to those skilled in the art, the term computer storage media includes volatile and non-volatile, removable and non-removable media implemented in any method or technology for storing information (such as computer-readable instructions, data structures, program modules, or other data). Computer storage media includes, but is not limited to, RAM, ROM, EEPROM, flash memory or other memory technologies, CD-ROM, digital versatile disc (DVD) or other optical disc storage, magnetic cartridges, magnetic tape, disk storage or other magnetic storage devices, or any other medium that can be used to store desired information and is accessible to a computer. Furthermore, as is known to those skilled in the art, communication media typically include computer-readable instructions, data structures, program modules, or other data in modulated data signals such as carrier waves or other transmission mechanisms, and may include any information delivery medium.
[0047] The above provides a detailed description of the preferred embodiments of the present invention. However, the present invention is not limited to the above embodiments. Those skilled in the art can make various equivalent modifications or substitutions without departing from the spirit of the present invention. All such equivalent modifications or substitutions are included within the scope defined by the claims of the present invention.< / name>
Claims
1. A method for parsing XML files, characterized in that, include: The target XML file is scanned, and the node index information of each node in the target XML file is read. A mapping file is constructed based on all the node index information, and the mapping file is associated with the target XML file. The node index information of any node includes the node identifier of the corresponding node, the position offset of the node identifier in the target XML file, and the node content length of the node. The memory usage of the mapping file is less than the memory usage of the target XML file. A parsing request for the target XML file is received, the parsing request including the target node; Obtain the target node identifier, target offset, and target content length corresponding to the target node from the mapping file; Target node data is obtained from the target XML file based on the target node identifier, target offset, and target content length. The target node data is parsed to obtain the node attribute information of the target node.
2. The XML file parsing method according to claim 1, characterized in that, The target XML file is scanned, and the node index information of each node in the target XML file is read, including: Load the target XML file into memory; When traversing the target XML file in memory, if the node start character of any node is detected, record the node identifier corresponding to any node, the position offset of the node identifier in the corresponding target XML file, and the node content length of the node. The node identifier, the position offset, and the node content length are determined as the node index information corresponding to the node.
3. The XML file parsing method according to claim 2, characterized in that, After recording the node identifier corresponding to any of the nodes, the position offset of the node identifier in the target XML file, and the node content length of the node, the method further includes: Record the pointer to the currently visited node; When the pointer is greater than the character length of the target XML file in the memory, the node index information of each node in the target XML file is read. If the pointer is less than the character length, continue to detect the node start character.
4. The XML file parsing method according to claim 1, characterized in that, The target node identifier, target offset, and target content length corresponding to the target node are obtained from the mapping file, including: The DOM tree is constructed from the mapped file using a DOM parser; Obtain the target node identifier, target offset, and target content length corresponding to the target node in the DOM tree.
5. An XML file parsing system, comprising: The mapping file generation module is used to scan the target XML file, read the node index information of each node in the target XML file, construct a mapping file based on all the node index information, and associate the mapping file with the target XML file. The node index information of any node includes the node identifier of the corresponding node, the position offset of the node identifier in the target XML file, and the node content length of the node. The memory usage of the mapping file is less than the memory usage of the target XML file. A parsing request receiving module is configured to receive a parsing request for the target XML file, wherein the parsing request includes a target node; The first data acquisition module is used to obtain the target node identifier, target offset and target content length corresponding to the target node from the mapping file; The second data acquisition module is used to acquire target node data from the target XML file based on the target node identifier, target offset, and target content length. The XML file parsing module is used to parse the target node data to obtain the node attribute information of the target node.
6. The XML file parsing system according to claim 4, characterized in that, The mapping file generation module includes: The first file loading module is used to load the target XML file into memory; The file scanning module is used to traverse the target XML file in the memory. When the node start character of any node is detected, the node identifier corresponding to any node, the position offset of the node identifier in the corresponding target XML file, and the node content length of the node are recorded. The node index information generation module is used to determine the node identifier, the position offset, and the node content length as the node index information of the corresponding node.
7. The XML file parsing system according to claim 4, characterized in that, The mapping file generation module further includes: The third data acquisition module is used to record the pointers of the currently traversed nodes; The first data processing module is used to read the node index information of each node in the target XML file when the pointer is greater than the character length of the target XML file in the memory; The second data processing module is used to continue detecting the node start character when the pointer is less than the character length.
8. The XML file parsing system according to claim 4, characterized in that, The first data acquisition module includes: The third data processing module is used to construct a DOM tree from the mapping file using a DOM parser; The fourth data acquisition module is used to obtain the target node identifier, target offset, and target content length corresponding to the target node in the DOM tree.
9. An XML file parsing system, characterized in that, It includes at least one control processor and a memory for communicatively connecting to the at least one control processor; the memory stores instructions executable by the at least one control processor, which, when executed by the at least one control processor, enable the at least one control processor to perform the XML file parsing method as described in any one of claims 1 to 4.
10. A computer-readable storage medium, characterized in that, The computer-readable storage medium stores computer-executable instructions for causing a computer to perform the XML file parsing method as described in any one of claims 1 to 4.