A method, device and medium for obtaining complete header information through offset record in SAX parsing XML scenario
By customizing the SAX content processor in SAX parsing XML, using offset records to obtain the document declaration information of the XML file, the problem of document declaration information loss in SAX parsing is solved, and the consistency between the translation and the original text is achieved.
Patent Information
- Application Number
- CN202411927691.X
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2024-12-25
- Publication Date
- 2025-04-08
AI Technical Summary
In the SAX parsing XML scenario, it is difficult for the existing technology to completely retain and output document declaration information of XML files, resulting in inconsistent translation and original text.
By customizing the SAX content processor in SAX parsing, rewriting the comment, endDTD, endCDATA, and startElement methods, using offset records to obtain the document declaration information of the XML file. The specific steps include updating the row and column offsets, judging the offset range, and traversing the characters to obtain the complete document declaration.
It realizes the complete acquisition of document declaration information of XML files in the SAX parsing XML scenario, avoids the loss of document declaration information and ensures the consistency between the translation and the original text.
Smart Images

Figure CN119783695A_ABST
Abstract
Description
Technical Field
[0001] The present invention belongs to the technical field of machine translation, and in particular relates to a method, device and medium for obtaining complete header information through offset records in a SAX parsing XML scenario. Background Art
[0002] In translation scenarios, XML files are usually required to have their output consistent with their input. Therefore, not only the node information of the XML file needs to be completely preserved, but also the document declaration of the XML file needs to be completely preserved and output to the translation file. XML is processed under the SAX framework, which can effectively control memory consumption. Moreover, SAX is event-driven and can obtain this information through a custom SAXContentHandler. However, this method will lose some XML document declarations, such as<?xml version="1.0" encoding="utf-8"?> Therefore, how to completely obtain the document declaration information of the XML file in the context of SAX parsing becomes a technical problem that needs to be solved urgently. Summary of the Invention
[0003] The purpose of the present invention is to provide a method for obtaining complete header information through offset records in a SAX parsing XML scenario, so as to solve the technical problems existing in the prior art.
[0004] In order to achieve the above object, the technical solution adopted by the present invention is as follows: A method for obtaining complete header information through offset records in a SAX parsing XML scenario includes the following steps: Step S1: Based on the event-driven custom SAX content processor of SAX parsing, rewrite the comment, endDTD, endCDATA, and startElement methods. Whenever any method is run, the row and column offset information is directly updated and counted as offset 1. Step S2: Rewrite the startElement method of SAXContentHandler to obtain the row and column offset information of the first occurrence of startElement, which is counted as offset 2; Step S3: Determine whether offset 1 and offset 2 satisfy: offset 1 ≤ offset 2. If so, execute step S4; if not, execute step S5. Step S4: Using offset 1 as the starting position and offset 2 as the ending position, traverse the characters appearing in the range, and update offset 1 obtained in step S1: row or / and column offset according to the traversal situation; Step S5: According to the offset 1 updated in step S4, obtain the document declaration of the XML file: according to the row offset information obtained in step S4, read the corresponding number of rows from the XML file; for the last row, intercept the corresponding number of characters according to the column offset information obtained in step S4. The character string obtained at this time is the complete document declaration of the XML file.
[0005] In one embodiment, in step S1, the custom SAX content processor includes custom attribute rows and columns.
[0006] In one embodiment, in step S1, the direct update method is to directly assign values to the user-defined attribute rows and columns.
[0007] In one embodiment, in step S4, during the traversal process, if a '<' character is encountered, the traversal is stopped, the row and column offsets are updated, and then step S5 is executed. If a non-'<' character is encountered, the column offset information obtained in step S1 is incremented by 1, and the traversal is continued. If a '\n' character is encountered, the row offset obtained in step S1 is increased by 1 and the column offset is set to 0, and then the process returns to step S3. If a non-'\n' character is encountered, the column offset is increased by 1, and then the process returns to step S3.
[0008] In one embodiment, in step S1, any method refers to any method among comment, endDTD, endCDATA, and startElement.
[0009] To achieve the above object, the present invention also provides a computer-readable storage medium having a computer program stored thereon, which is executed by a processor to implement the method of obtaining complete header information through offset records in the SAX parsing XML scenario.
[0010] In order to achieve the above-mentioned purpose, the present invention also provides a device for obtaining complete header information through offset records in a SAX parsing XML scenario, comprising: a processor and a memory; the memory is used to store a computer program; the processor is connected to the memory and is used to execute the computer program stored in the memory, so that the device for obtaining complete header information through offset records in a SAX parsing XML scenario executes the method for obtaining complete header information through offset records in a SAX parsing XML scenario.
[0011] Compared with the prior art, the present invention has the following beneficial effects: Based on the technical solution provided by the present invention, when SAX is used to parse XML, the document declaration information of the entire XML file can be completely obtained without loss of document declaration information, thereby ensuring the consistency of the translation with the original text. BRIEF DESCRIPTION OF THE DRAWINGS
[0012] Figure 1 This is a schematic diagram of the process of Example 1 of the present invention.
[0013] Figure 2 This is a principle block diagram of Example 3 of the present invention. DETAILED DESCRIPTION
[0014] In order to enable those skilled in the art to have a clearer understanding and knowledge of the present invention, the present invention will be further described in detail below with reference to the embodiments. It should be understood that the specific embodiments described below are only used to explain the present invention and facilitate understanding. The technical solutions provided by the present invention are not limited to the technical solutions provided by the following embodiments, and the technical solutions provided by the embodiments should not limit the scope of protection of the present invention.
[0015] It should be noted that the illustrations provided in the following embodiments are only schematic illustrations of the basic concept of the present invention. Therefore, the drawings only show components related to the present invention and are not drawn according to the number, shape and size of components in actual implementation. In actual implementation, the shape, quantity and proportion of each component can be changed at will, and the component layout may also be more complicated.
[0016] Example 1 like Figure 1 As shown, this embodiment provides a method for obtaining complete header information through offset records in a SAX parsing XML scenario. The design principle of this method is as follows: an event-driven custom SAX content processor based on SAX parsing is used to rewrite the comment, endDTD, endCDATA, and startElement methods. When the event-driven method comment, endDTD, endCDATA, etc. is called, the row, column, and offset information of the XML file at this time are recorded (SAX will only read 8192 bytes of data when reading a file, and this offset is based on the offset of the current byte array); when the event-driven method calls the startElement method for the first time, the row, column, and offset information at this time are recorded; the space occupied by the document declaration information of the entire XML file is calculated through the two row and column information, and finally the complete document declaration information is obtained through the row and column information.
[0017] 1. Rewrite comment, endDTD, endCDATA, startElement methods An event-driven custom SAX content processor based on SAX parsing rewrites the comment, endDTD, endCDATA, and startElement methods. Whenever any method is run, the row and column offset information is directly updated, and the offset is counted as 1. The custom SAX content processor contains two custom attributes: row and column. Based on this design, the method to directly update the row and column offset information is to directly assign values to the row and column offsets.
[0018] 2. Rewrite the startElement method of SAXContentHandler Override the startElement method of SAXContentHandler to obtain the row and column offset information where the startElement first appears, and count it as offset 2.
[0019] 3. Judgment steps Based on the offset 1 and offset 2 obtained in step 1 and step 2, determine whether offset 1 and offset 2 satisfy: offset 1 ≤ offset 2. If so, execute step S4; if not, execute step S5.
[0020] 4. Calculate the space occupied by document declaration The row and column information obtained in step 1 is only a rough estimate and does not fully capture the document declaration information. Step 2 obtains the row, column, and offset of the first element after reading it. Using offset 1 as the starting position and offset 2 as the ending position, iterate through the characters in the range and update offset 1 obtained in step 1 based on the traversal results: the row and / or column offsets. Updating directly assigns values to both the row and column offsets. The resulting row, column, and offset represent the space occupied by the entire XML document declaration relative to the entire XML file.
[0021] During the traversal process, if a '<' character is encountered, the traversal is stopped, the row and column offsets are updated, and then the next step is executed. If a non-'<' character is encountered, the column offset information in step 1 is increased by 1, and then the traversal is continued; if a '\n' character is encountered, the obtained row offset is increased by 1 and the column offset is set to 0, and then the judgment step is returned. If a non-'\n' character is encountered, the column offset is increased by 1, and then the judgment step is returned.
[0022] 5. Get the document declaration of the XML file According to the offset 1 updated in step 4, the document declaration of the XML file is obtained as follows: according to the row offset information obtained in step 4, the corresponding number of rows is read from the XML file; for the last row, the corresponding number of characters is intercepted according to the column offset information obtained in step 4. The string obtained at this time is the complete document declaration of the XML file.
[0023] Based on the above configuration of this embodiment, when SAX is used to parse XML, the document declaration information of the entire XML file can be completely obtained without loss of document declaration information, thereby ensuring consistency between the translation and the original text.
[0024] Example 2 This embodiment provides a computer-readable storage medium having a computer program stored thereon, which is executed by a processor to implement the method for obtaining complete header information through offset records in the SAX parsing XML scenario provided in Example 1. Those skilled in the art will understand that all or part of the steps of the method provided in Example 1 can be completed by hardware associated with the computer program, and the aforementioned computer program can be stored in a computer-readable storage medium. When executed, the program performs the steps of the method provided in Example 1. The aforementioned storage medium includes various media capable of storing program code, such as ROM, RAM, magnetic disk, or optical disk.
[0025] Example 3 like Figure 2 As shown, this embodiment provides a device for obtaining complete header information through offset records in a SAX parsing XML scenario, including: a processor and a memory; the memory is used to store a computer program; the processor is connected to the memory, and is used to execute the computer program stored in the memory, so that the device for obtaining complete header information through offset records in a SAX parsing XML scenario executes the method for obtaining complete header information through offset records in a SAX parsing XML scenario provided in Example 1.
[0026] Specifically, the memory includes various media that can store program codes, such as ROM, RAM, magnetic disk, USB flash drive, memory card or optical disk.
[0027] Preferably, the processor can be a general-purpose processor, including a central processing unit, a network processor, etc.; it can also be a digital signal processor, an application-specific integrated circuit, a field programmable gate array or other programmable logic device, a discrete gate or transistor logic device, or a discrete hardware component.
[0028] The above embodiments are merely illustrative of the principles and effects of the present invention and are not intended to limit the present invention. Anyone skilled in the art may modify or alter the above embodiments without departing from the spirit and scope of the present invention. Therefore, all equivalent modifications or alterations made by one of ordinary skill in the art without departing from the spirit and technical principles disclosed herein are intended to be covered by the claims of the present invention.
Claims
1. A method for obtaining complete header information through offset records in a SAX parsing XML scenario, characterized in that: The following steps are involved: Step S1: Based on the event-driven custom SAX content processor of SAX parsing, rewrite the comment, endDTD, endCDATA, and startElement methods. Whenever any method is run, the information of the row and column offsets is directly updated and counted as offset 1. Step S2: rewrite the startElement method of SAXContentHandler to obtain the information of the row and column offsets where the startElement first appears, which is counted as offset 2; Step S3: Determine whether offset 1 and offset 2 satisfy: offset 1 ≤ offset 2. If yes, execute step S4; if no, execute step S5. Step S4: using offset 1 as the starting position and offset 2 as the ending position, traverse the characters appearing in the range, and update offset 1 obtained in step S1 according to the traversal situation: row or / and column offset; Step S5: According to the offset 1 updated in step S4, obtain the document declaration of the XML file: according to the row offset information obtained in step S4, read the corresponding number of rows from the XML file; for the last row, intercept the corresponding number of characters according to the column offset information obtained in step S4. The character string obtained at this time is the complete document declaration of the XML file.
2. The method for obtaining complete header information by using offset records in a SAX parsing XML scenario according to claim 1, characterized in that: In step S1, the custom SAX content processor includes custom attribute rows and columns.
3. The method for obtaining complete header information by using offset records in a SAX parsing XML scenario according to claim 2, characterized in that: In step S1, the direct update method is to directly assign values to the user-defined attribute rows and columns.
4. The method for obtaining complete header information by using offset records in a SAX parsing XML scenario according to claim 3, characterized in that: In step S4, during the traversal process, if a '<' character is encountered, the traversal is stopped, and the row and column offsets are updated, and then step S5 is executed. If a non-'<' character is encountered, the column offset information obtained in step S1 is increased by 1, and then the traversal is continued; If a '\n' character is encountered, the row offset obtained in step S1 is increased by 1 and the column offset is set to 0, and then the process returns to step S3. If a non-'\n' character is encountered, the column offset is increased by 1, and then the process returns to step S3.
5. The method for obtaining complete header information by using offset records in a SAX parsing XML scenario according to claim 4, characterized in that: In the step S1, any method refers to any method among the comment, endDTD, endCDATA, and startElement.
6. A computer-readable storage medium having a computer program stored thereon, characterized in that: The computer program is executed by a processor to implement the method for obtaining complete header information through offset records in a SAX parsing XML scenario as described in any one of claims 1 to 5.
7. A device for obtaining complete header information through offset records in a SAX parsing XML scenario, characterized in that: include: Processor and memory; The memory is used to store computer programs; the processor is connected to the memory and is used to execute the computer programs stored in the memory, so that the device for obtaining complete header information through offset records in the SAX parsing XML scenario executes the method for obtaining complete header information through offset records in the SAX parsing XML scenario as described in any one of claims 1 to 5.