XML (Extensible Markup Language) text parser, wrapper, system and method
Through the component design of XML text parser and encapsulator, using string operations to achieve rapid parsing and encapsulation of XML text, solving the high learning cost, large memory usage and security risks of existing tools, improving efficiency and reducing dependency The demand for three-party tools.
Patent Information
- Application Number
- CN202411326660.9
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2024-09-23
- Publication Date
- 2025-01-07
AI Technical Summary
The existing XML text parsing and encapsulation tools have high learning costs, large memory usage, low efficiency, and security risks.
Provide an XML text parser and encapsulator, which can parse XML text through text conversion components, key positioning components and value acquisition components, and XML text encapsulation through text template loading components and template replacement components, and use string operations to achieve rapid parsing and Encapsulation, reducing dependence on XML language features.
Without relying on third-party tools, the rapid parsing and encapsulation of XML text is achieved, reducing memory usage and learning costs, while avoiding security risks and improving efficiency.
Smart Images

Figure CN119272724A_ABST
Abstract
Description
Technical Field
[0001] The present invention relates to the field of computer technology, and in particular to an XML text parser, encapsulator, system and method. Background Art
[0002] Extensible Markup Language XML is widely used in the fields of text interaction and parameter configuration of application systems, including the application of XML under the common Web Service standard and the use of XML text in other custom formats.
[0003] XML text is a key-value text format. The key can be either the tag value of the XML text or the attribute value of the XML text, and the value is the content between two tags. <key1> a< / key1> ", the key can be the tag value "key1", and the value is a; for the text of the form "<ele id=’key2’> b", its key can be the attribute value "key2" of the tag value ele attribute id, and its value is b.
[0004] There are currently a variety of third-party tools that can parse and encapsulate XML texts. Since they fully support XML language features, they have a high learning cost and there are security risks such as XXE injection. The so-called XXE injection can be referred to "Wei Yufeng, Hang Xiao. A Brief Discussion on XXE Attacks and Prevention Measures [J]. Network Security Technology and Applications, 2022.". XML text parsing can only be used to obtain the value in the XML text, and XML text encapsulation can only be used to map and replace key-value values according to the XML format. Therefore, XML text parsing and encapsulation do not need to use all XML language features, and there is no need to parse the XML text structure. Therefore, third-party tools also have large memory usage and low efficiency when parsing and encapsulating XML texts. Summary of the invention
[0005] In view of the problems of high learning cost, large memory usage, low efficiency and potential safety hazards existing in current third-party tools for XML parsing and encapsulation, the present invention provides an XML text parser, encapsulator, system and method.
[0006] In a first aspect, the present invention provides an XML text parser, comprising:
[0007] A text conversion component, used for obtaining XML text and converting the XML text into a string;
[0008] The key location component is used to obtain the position where the key first appears in the string;
[0009] The value acquisition component is used to locate the value in the string after the key first appears in the string.
[0010] Furthermore, the value acquisition component is specifically used to:
[0011] After the first occurrence of key in the string, find the position pos1 of the first ">" and the position pos2 of the first "<", and use the content in the interval [pos1,pos2) as the value in the string.
[0012] In a second aspect, the present invention provides an XML text encapsulator, comprising:
[0013] A text template loading component, used to load a corresponding text template according to a given text template type, and output a character string of the text template;
[0014] The template replacement component is used to receive a replacement value, and replace the key value in the character string of the text template with the replacement value to generate the XML text required by the application system.
[0015] Furthermore, the text template includes a static block and a block to be replaced;
[0016] The static block is used to maintain the original text format and content and encapsulate the text content in the final XML text;
[0017] The block to be replaced is used to carry the key value to be replaced.
[0018] In a third aspect, the present invention provides an application system, comprising the XML text parser as described in the first aspect and the XML text encapsulator as described in the second aspect.
[0019] In a fourth aspect, the present invention provides an XML text parsing method, comprising:
[0020] Obtain XML text and convert the XML text into a string;
[0021] Get the position where key first appears in the string;
[0022] After the first occurrence of key in the string, locate the value in the string.
[0023] In a fifth aspect, the present invention provides an XML text encapsulation method, comprising:
[0024] Load the corresponding text template according to the given text template type, and output the string of the text template;
[0025] A replacement value value is received, and the key value in the character string of the text template is replaced with the replacement value value to generate an XML text required by the application system.
[0026] Beneficial effects of the present invention:
[0027] Based on clarifying that XML text is in key-value text format and defining the range of the key, the present invention can quickly complete the rapid parsing and encapsulation of XML text only through string operations without relying on third-party tools, which can effectively reduce memory usage and learning costs, and avoid security risks existing in third-party tools while improving efficiency. BRIEF DESCRIPTION OF THE DRAWINGS
[0028] Figure 1 A schematic diagram of the structure of an XML text parser provided by an embodiment of the present invention;
[0029] Figure 2 A schematic diagram of the structure of an XML text encapsulator provided by an embodiment of the present invention;
[0030] Figure 3 A schematic diagram of the structure of an application system provided by an embodiment of the present invention. DETAILED DESCRIPTION
[0031] In order to make the purpose, technical solution and advantages of the present invention clearer, the technical solution in the embodiment of the present invention will be clearly described below in conjunction with the drawings in the embodiment of the present invention. Obviously, the described embodiment is a part of the embodiment of the present invention, not all the embodiments. Based on the embodiments of the present invention, all other embodiments obtained by ordinary technicians in this field without creative work are within the scope of protection of the present invention.
[0032] like Figure 1 As shown, an embodiment of the present invention provides an XML text parser, including a text conversion component, a key positioning component and a value acquisition component.
[0033] The text conversion component is used to obtain XML text and convert the XML text into a string; the key positioning component is used to obtain the position where the key first appears in the string; the value acquisition component is used to locate the value in the string after the key first appears in the string.
[0034] Specifically, the text parsing process of the XML text parser is as follows: after the text conversion component receives the XML text from the application system, it converts the XML text into a string and outputs it to the key positioning component; the key positioning component obtains the position where the key first appears in the string, and transmits the result to the value acquisition component; finally, the value acquisition component locates the value in the string after the key first appears in the string.
[0035] In this embodiment, the key positioning component supports obtaining <key> value< / key> ,<ele id=”key”> ,<key id=”id1”> value,<ele key a> value,<ele key> The position of the first occurrence of key in a string of format such as value.
[0036] The value acquisition component searches for the first position of the “>” (pos1) and the first position of the “<” (pos2) after the first appearance of the key, and obtains the content of the string in the interval [pos1,pos2), which is the value in the string.
[0037] In practical applications, it can be understood that the key positioning component can be expressed in computer pseudocode as int locate(String key), the input value is the key to obtain the value, and the return value is the position where the key first appears in the string. The value acquisition component can be expressed in computer pseudocode as String locate(int pos), the input value is the position where the key first appears in the string, and the return value is the value to be obtained.
[0038] like Figure 2 As shown, an embodiment of the present invention provides an XML text encapsulator, including a text template loading component and a template replacement component.
[0039] Among them, the text template loading component is used to load the corresponding text template according to the given text template type, and output the string of the text template; the template replacement component is used to receive the replacement value value, and replace the key value in the string of the text template with the replacement value value to generate the XML text required by the application system.
[0040] Specifically, the text encapsulation process of the XML text encapsulator includes: the text template loading component loads the corresponding text template according to the given text template type, and outputs the string of the text template to the template replacement component; the template replacement component is used to receive the replacement value value, and replace the key value in the string of the text template with the replacement value value to generate the XML text required by the application system.
[0041] In this embodiment, the text template includes a static block and a block to be replaced; the static block is used to maintain the original text format and content, and is encapsulated into the text content in the final XML text; the block to be replaced is used to carry the key value to be replaced, which can be defined as " <key>< / key> " format.
[0042] In practical applications, it can be understood that the text template loading component can be represented by computer pseudo code as String template Load (String template Type), the input value is the text template type, and the return value is the string of the text template. The template replacement component can be represented by computer pseudo code as String replace (String template, String key, String replace String), the input value is the string of the text template, the key in the key value to be replaced, and the replacement value, and the return value is the string of the replaced text template.
[0043] like Figure 3 As shown, an embodiment of the present invention further provides an application system, including an XML text parser and an XML text encapsulator.
[0044] Specifically, when it is necessary to obtain the value list, the XML text is converted into a string through the XML text parser, and the “<;” and “>;” in the text are replaced, with “<;” replaced with “<” and “>;” replaced with “>”, and the key positioning component and the value acquisition component are called in a loop according to the key list to finally complete the acquisition of the value list.
[0045] When the application system needs to generate XML text, the text template is loaded through the text template loading component in the XML text encapsulator, and the key values to be replaced in the text template are replaced in sequence through the template replacement component, and finally the generation of the XML text is completed.
[0046] In order to verify the effectiveness of this solution, the following is a further explanation based on actual cases:
[0047] Case 1: The XML text is shown in Table 1. The string obtained through the text conversion component is shown in Table 2. The key is defined as "to", the position obtained by the key positioning component is 46, and the value obtained by the value acquisition component is Tove.
[0048] Table 1
[0049]
[0050] Table 2
[0051]
[0052] Case 2: XML text template As shown in Table 3, after being loaded by the text template loading component, the keys are defined as "toBeReplaced1" and " <from>< / from> ", the block to be replaced is " <to> toBeReplaced1< / to> "," <from>< / from> ", and other text content is a static block. Define the value values as "example1" and " <from> example2< / from> " and input it into the template replacement component, the XML text finally generated is shown in Table 4.
[0053] Table 3
[0054]
[0055] Table 4
[0056]
[0057] The XML text parser and XML text encapsulator provided by the present invention have no dependency relationship and can be used independently or in combination. The XML text parser can obtain the corresponding value according to the key, and the XML text encapsulator can generate the corresponding XML text content according to the mapping of the key-value value. On the basis of clarifying that the XML text is a key-value type text format and defining the range of the key, the present invention can complete the rapid parsing and encapsulation of the XML text only through string operations without relying on third-party tools.
[0058] Finally, it should be noted that the above embodiments are only used to illustrate the technical solutions of the present invention, rather than to limit it. Although the present invention has been described in detail with reference to the aforementioned embodiments, those skilled in the art should understand that they can still modify the technical solutions described in the aforementioned embodiments, or make equivalent replacements for some of the technical features therein. However, these modifications or replacements do not deviate the essence of the corresponding technical solutions from the spirit and scope of the technical solutions of the embodiments of the present invention.
Claims
1. An XML text parser, characterized in that: include: A text conversion component, used for obtaining XML text and converting the XML text into a string; The key location component is used to obtain the position where the key first appears in the string; The value acquisition component is used to locate the value in the string after the key first appears in the string.
2. An XML text parser according to claim 1, characterized in that: The value acquisition component is specifically used for: After the first occurrence of key in the string, find the position pos1 of the first ">" and the position pos2 of the first "<", and use the content in the interval [pos1,pos2) as the value in the string.
3. An XML text encapsulator, characterized in that: include: A text template loading component, used to load a corresponding text template according to a given text template type, and output a character string of the text template; The template replacement component is used to receive a replacement value, and replace the key value in the character string of the text template with the replacement value to generate the XML text required by the application system.
4. An XML text encapsulator according to claim 3, characterized in that: The text template includes a static block and a block to be replaced; The static block is used to maintain the original text format and content and encapsulate the text content in the final XML text; The block to be replaced is used to carry the key value to be replaced.
5. An application system, characterized in that: It comprises the XML text parser as claimed in claim 1 or 2 and the XML text encapsulator as claimed in claim 3 or 4.
6. An XML text parsing method, characterized in that: include: Obtain XML text and convert the XML text into a string; Get the position where key first appears in the string; After the first occurrence of key in the string, locate the value in the string.
7. An XML text encapsulation method, characterized in that: include: Load the corresponding text template according to the given text template type, and output the string of the text template; A replacement value value is received, and the key value in the character string of the text template is replaced with the replacement value value to generate an XML text required by the application system.