Method and system for comparing structured data files

By constructing a rule index and using multi-dimensional similarity calculation, the problem of redundant data in structured data file comparison is solved, achieving flexible and accurate comparison results, which is particularly suitable for configuration file version management and data migration verification.

CN123158246APending Publication Date: 2026-11-24CHINESE PEOPLES LIBERATION ARMY UNIT 63791
View PDF 0 Cites 0 Cited by

Patent Information

Application Number
CN202611343366.8
Authority / Receiving Office
CN · China
Patent Type
Applications(China)
Current Assignee / Owner
Filing Date
2026-09-01
Publication Date
2026-11-24

AI Technical Summary

Technical Problem

Existing text comparison technologies tend to generate redundant data when comparing structured data files, and cannot filter out unimportant attributes or elements according to business needs, resulting in inaccurate difference reports.

Method used

By parsing the baseline configuration file to extract the whitelist rule list, constructing a rule index, and recursively comparing node pairs based on the rule index, multiple rule types are flexibly configured to calculate multi-dimensional similarity and generate a comprehensive comparison report.

Benefits of technology

It enables highly targeted and flexible structured data file comparison, reduces redundant data, and improves the accuracy and simplicity of comparison results. It is suitable for intelligent comparison of tree-structured data formats such as XML, JSON, and YAML.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN123158246A_ABST
    Figure CN123158246A_ABST
Patent Text Reader

Abstract

This invention relates to a method and system for comparing structured data files. The method includes the following steps: S1, parsing a baseline configuration file to extract a whitelist rule list, and constructing a rule index based on the whitelist rule list; S2, parsing two files to be compared into a first node tree and a second node tree, respectively; S3, initializing the comparison state; S4, taking nodes at corresponding positions in the first node tree and the second node tree as node pairs, and recursively comparing the node pairs based on the rule index; S5, after the comparison is completed, calculating similarity from multiple dimensions and generating a comprehensive comparison report. This invention solves the problems of unreasonable similarity calculation, inflexible rule application, and rigid node matching strategies in conventional text comparison technologies. It supports multiple formats such as XML, JSON, and YAML, provides multi-dimensional similarity evaluation, and is suitable for scenarios such as configuration management, data migration verification, and API testing.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] This invention relates to the field of data analysis technology, and in particular to a method and system for comparing structured data files. Background Technology

[0002] Structured data formats such as XML and JSON are common data representations in the computer field, widely used in configuration management, data storage, and information transmission. In practical applications, it is often necessary to compare two different versions of structured data files to identify differences and assess similarity. Examples include: version comparison of software configuration files, consistency verification before and after data migration, and format validation of API return values. Common text comparison techniques typically employ a full comparison approach. While this method yields accurate results, it is prone to generating redundant data and cannot filter out unimportant attributes or elements based on business needs. For instance, in configuration file comparisons, fields such as timestamps and log levels frequently change but do not affect the judgment of substantial differences. Existing text comparison methods cannot intelligently ignore these fields, resulting in a large number of meaningless difference reports. Summary of the Invention

[0003] The purpose of this invention is to provide a method and system for comparing structured data files, so as to improve the problem that existing text comparison methods are prone to generating redundant data.

[0004] To achieve the above objectives, the present invention provides the following technical solution:

[0005] A method for comparing structured data files includes the following steps:

[0006] S1, parse the baseline configuration file to extract the whitelist rule list, and build a rule index based on the whitelist rule list;

[0007] S2, parse the two files to be compared into a first node tree and a second node tree respectively;

[0008] S3, Initialize the comparison state, including initializing the total weight variable and the matching weight variable;

[0009] S4, take the nodes at corresponding positions in the first node tree and the second node tree as node pairs, and recursively compare the node pairs based on the rule index;

[0010] S5: After the comparison is completed, the similarity is calculated from multiple dimensions and a comprehensive comparison report is generated.

[0011] In step S41, if only one node exists, the process of recording the difference information includes:

[0012] If the first node does not exist but the second node does, then: mark the difference type as "extra", indicating that there are extra nodes in the second file to be compared; obtain the weight value of the second node and add it to the total weight variable; add the difference information to the difference list, which includes the node path, difference type, node count, and weight value; do not continue to recursively compare the child nodes of the current node, and return directly;

[0013] If the first node exists but the second node does not, then: query the rule index to obtain the rule list corresponding to the node path of the first node; determine whether there is a FILTER type rule in the rule list. If there is a FILTER type rule, the node is considered to be filtered, the weight value of the first node is added to the matching weight variable, the difference information is not recorded, and the process is returned directly; if there is no FILTER type rule, the difference type is marked as "missing", indicating that the node in the first file to be compared is missing in the second file to be compared. The weight value of the first node is obtained, added to the total weight variable, and the difference information is added to the difference list. The process does not continue to recursively compare the child nodes of the current node and returns directly.

[0014] A system for comparing structured data files includes the following modules:

[0015] The rule index building module is used to parse the baseline configuration file to extract the whitelist rule list and build a rule index based on the whitelist rule list;

[0016] The node tree generation module is used to parse two files to be compared into a first node tree and a second node tree, respectively.

[0017] The initialization module is used to initialize the alignment state, including initializing the total weight variable and the matching weight variable;

[0018] The recursive comparison module is used to take the nodes at corresponding positions in the first node tree and the second node tree as node pairs, and recursively compare the node pairs based on the rule index.

[0019] The similarity calculation module is used to calculate similarity from multiple dimensions after the comparison is completed and generate a comprehensive comparison report.

[0020] A computer program product includes computer-readable instructions that, when executed by a processor, implement the steps in the method for comparing structured data files.

[0021] A computer-readable storage medium including computer-readable instructions that, when executed by a processor, implement the steps in the method for comparing structured data files.

[0022] An electronic device includes: a memory storing program instructions; and a processor connected to the memory to execute the program instructions in the memory, thereby implementing the steps in the structured data file comparison method.

[0023] Compared with existing technologies, this invention solves the problems of unreasonable similarity calculation, inflexible rule application, and rigid node matching strategies in conventional text content comparison technologies. By summarizing multiple rule types and using different combinations of different types, it achieves flexible configuration of differentiated comparison rule execution processes based on the characteristics of each node's data. It is highly targeted and flexible, and is suitable for intelligent comparison and similarity calculation of tree-structured data formats such as XML, JSON, and YAML. It is particularly suitable for application scenarios such as configuration file version management and data migration verification. Attached Figure Description

[0024] Figure 1 A flowchart of a method for comparing structured data files provided for an embodiment.

[0025] Figure 2a , Figure 2b , Figure 2c , Figure 2d , Figure 2e Together, they form the detailed flowchart of the recursive comparison nodes in the embodiment.

[0026] Figure 3 This is a block diagram illustrating the components of a structured data file comparison system as described in this embodiment.

[0027] Figure 4 This is a block diagram of the electronic device provided in the embodiment. Detailed Implementation

[0028] The technical solution of the present invention will be further described below with reference to the accompanying drawings and specific embodiments.

[0029] like Figure 1 As shown in the figure, this embodiment provides a method for comparing structured data files, which includes the following steps:

[0030] S1, parse the baseline configuration file to extract the whitelist rule list, and build a rule index based on the whitelist rule list;

[0031] S2, parse the two files to be compared into a first node tree and a second node tree respectively;

[0032] S3, Initialize the comparison state;

[0033] S4, take the nodes at corresponding positions in the first node tree and the second node tree as node pairs, and recursively compare the node pairs based on the rule index;

[0034] S5: After the comparison is completed, the similarity is calculated from multiple dimensions and a comprehensive comparison report is generated.

[0035] In this embodiment, step S1 may specifically include the following sub-steps:

[0036] S11, Obtain the baseline configuration file. This baseline configuration file defines the whitelist rules required for the current comparison task. Each rule includes the rule path, field name, rule type, and rule description. Some rules also include rule values ​​or weight values.

[0037] The rule types summarized in this invention include eight types. However, the rules required for the comparison process differ depending on the characteristics of the structured data files. Therefore, before executing the comparison task, it is necessary to extract the rules required for the current comparison task from the eight rule types. In other words, it is necessary to manually define the rules required for the current comparison task in advance and generate the benchmark configuration file.

[0038] The eight rule types mentioned include:

[0039] FILTER (Filtering Rules): Specifies the fields to be ignored during the comparison process;

[0040] EQUALS (equals rules): Specifies a mandatory requirement that a field must be equal to a specific value;

[0041] RANGE (range rule): Specifies that numeric fields must be within a specific range;

[0042] REGEX (regular expression rule): Specifies that a field value must match the specified regular expression;

[0043] IGNORE_ORDER (Ignore Order Rules): Specifies that the order of the child node set does not affect the matching result;

[0044] FUZZY (fuzzy matching rule): Specifies that a certain degree of difference is allowed;

[0045] WEIGHT (weighting rule): Specifies the weighting coefficient of a node or field in the similarity calculation;

[0046] TRANSFORM (Transformation Rule): Specifies a function to perform a normalization transformation on field values ​​before comparison.

[0047] Different rules are applicable to data comparison with different characteristics. By calling different rules for comparison of different data, it is more targeted and flexible than fixed content consistency comparison, thereby improving the accuracy and simplicity of the comparison results.

[0048] S12, parse the baseline configuration file and extract the whitelist rule list;

[0049] S13: Construct a rule index based on the whitelist rule list. The rule index adopts a hash table structure, using node paths as keys and rule lists as values, to achieve rule lookup with O(1) time complexity.

[0050] In this embodiment, step S2 may include the following sub-steps:

[0051] S21: Obtain a first file to be compared and a second file to be compared;

[0052] S22: Identify file formats of the first file to be compared and the second file to be compared;

[0053] In this step, file extensions are first checked, and a preliminary judgment is made based on the file extensions (preset extension list);

[0054] Then, if the format cannot be determined from the extension or the extension is not in the preset extension list, the first few bytes of the file are read, and the format is determined based on the characteristics of the first few bytes. For example, if the content starts with "<?xml", it is determined as XML format; if the content starts with "{" or "[", it is determined as JSON format; if the content conforms to YAML syntax characteristics (such as containing indentation and colon-separated key-value pairs), it is determined as YAML format; other formats are analogized;

[0055] Finally, if the format still cannot be determined, an error message indicating format identification failure is returned. Thereafter, the file format may be manually judged.

[0056] S23: Invoke a corresponding parser according to the identified file format, parse the first file to be compared into a first node tree, and parse the second file to be compared into a second node tree.

[0057] The first / second node tree adopts a unified node data structure, and each node includes:

[0058] Node path: complete path identifier of the node in the tree;

[0059] Node name: name identifier of the node;

[0060] Node attributes: a set of attribute key-value pairs included in the node;

[0061] Node value: content value of a leaf node;

[0062] Child node list: a list of child node objects;

[0063] Node count (node_count): The total number of nodes in the subtree rooted at the current node.

[0064] In this embodiment, step S3, initializing the comparison state includes initializing the variables that need to be statistically analyzed, initializing the comparison queue, and constructing the path mapping table.

[0065] The variables that need to be statistically analyzed include total weight, matching weight, total number of nodes, and difference list; initializing the statistical variables means setting the total weight, matching weight, and total number of nodes to 0, and setting the difference list to empty.

[0066] Initializing the alignment queue involves adding the root nodes of the first node tree and the second node tree as the initial alignment node pairs to the alignment queue.

[0067] Building a path mapping table establishes a mapping relationship between node paths and node objects, which is used to quickly locate nodes.

[0068] Because the flowchart for the recursive comparison is too large, it has been split into five local diagrams, namely: Figure 2a , Figure 2b , Figure 2c , Figure 2d and Figure 2e The letters A / B / C / D / E / F in the diagram represent cut points, used to indicate the splicing method between different local images.

[0069] For reference Figure 2a , Figure 2b , Figure 2c , Figure 2d , Figure 2e In this embodiment, in step S4, for each pair of nodes to be compared, the following sub-steps are performed:

[0070] S41, determine whether the two nodes to be compared exist, and perform corresponding processing based on the judgment result.

[0071] For ease of description, the two nodes to be compared are referred to as the first node and the second node, respectively. The first node comes from the first node tree and the second node comes from the second node tree. Usually, the first file to be compared is used as the standard file and the second file to be compared is used as the file to be compared with.

[0072] Determining the existence of the first and second nodes involves handling the following four cases:

[0073] (1) If neither the first node nor the second node exists, return directly;

[0074] The absence of both the first and second nodes means that no nodes to be compared were found in either the first or second comparison file. This indicates that there are more rules configured than actually configured. Therefore, this comparison does not need to be executed, so the comparison ends directly, returns to the main process, and proceeds to the comparison of the next node.

[0075] Redundant rules or incorrect configuration by the personnel can cause nodes to not exist. This condition is set to prevent the process from failing to continue in this situation.

[0076] (2) If the first node does not exist but the second node does exist, then:

[0077] Mark the difference type as "extra" to indicate that there are extra nodes in the second file to be compared;

[0078] Obtain the weight value of the second node and add it to the total weight variable. The weight value of the node is defined in the weight rules in the whitelist rules. Its size is preset manually according to business needs. For example, if the business execution is more sensitive to the value of node A, then the weight needs to be set to be larger. In this way, when node A changes, the difference will be reflected in the comparison results.

[0079] Add the difference information to the difference list. The difference information includes node path, difference type, node count, and weight value.

[0080] Instead of recursively comparing the child nodes of the current node, return directly;

[0081] (3) If the first node exists but the second node does not exist, then:

[0082] Query the rule index to obtain the rule list corresponding to the node path of the first node;

[0083] Determine whether a FILTER type rule exists in the rule list. If a FILTER type rule exists, the node is considered filtered, the weight value of the first node is added to the matching weight variable, the difference information is not recorded, and the process is returned directly. If no FILTER type rule exists, the difference type is marked as "missing", indicating that the node in the first comparison file is missing in the second comparison file. The weight value of the first node is obtained, added to the total weight variable, and the difference information is added to the difference list. The process does not continue to recursively compare the child nodes of the current node and returns directly.

[0084] (4) If both the first node and the second node exist, then continue to execute step S42.

[0085] S42, determine whether the node names are equal and whether the node paths are equal, and perform corresponding processing based on the judgment results.

[0086] Determine whether the node name of the first node is equal to the node name of the second node, and whether the node path of the first node is equal to the node path of the second node;

[0087] If the node names are not equal or the node paths are not equal, then:

[0088] Mark the difference type as "name_diff" to indicate node name differences;

[0089] Obtain the weight value of the first node and add it to the total weight variable;

[0090] Add the difference information to the difference list. The difference information includes the node path, difference type, first node name, second node name, and weight value.

[0091] Instead of recursively comparing the child nodes of the current node, return directly;

[0092] If the node name and node path are both equal, then proceed to step S43.

[0093] S43, Apply EQUALS rules to validate the mandatory rules.

[0094] Query the rule index to obtain the rule list corresponding to the current node path, filter out the EQUALS type rules, and for each EQUALS type rule, perform the following operations:

[0095] Retrieve the field name and expected value from the rules;

[0096] Retrieve the actual value of this field from the attribute set of the first node, and denote it as actual value 1;

[0097] Retrieve the actual value of this field from the attribute set of the second node, and denote it as actual value 2;

[0098] Determine whether actual value 1 equals the expected value, and whether actual value 2 equals the expected value. If neither actual value equals the expected value, then:

[0099] Mark the difference type as "equals_rule_violation" to indicate a violation of a mandatory rule;

[0100] Obtain the weight value of the first node, multiply it by 2 as a penalty coefficient, and add it to the total weight variable;

[0101] Add the difference information to the difference list. The difference information includes node path, difference type, field name, expected value, actual value 1, actual value 2, and weight value.

[0102] Instead of recursively comparing the child nodes of the current node, return directly;

[0103] If all EQUALS type rules satisfy the condition that actual value 1 and actual value 2 are both equal to the expected value, then continue to execute step S44.

[0104] If there are no rules of type EQUALS in the rule list, proceed directly to step S44.

[0105] S44, compare node attributes using FILTER type rules.

[0106] Query the rule index to obtain the rule list corresponding to the current node path, and filter out the FILTER type rules; if there are no FILTER type rules in the rule list, proceed directly to step S45;

[0107] Extract the names of the fields to be filtered from the FILTER type rules and construct a set of filtered fields;

[0108] Remove the fields from the filter field set from the attribute set of the first node to obtain the effective attribute set 1;

[0109] Remove the fields from the filter field set from the attribute set of the second node to obtain the effective attribute set 2;

[0110] Merge all keys from valid attribute set 1 and valid attribute set 2 to obtain the set of attribute keys to be compared;

[0111] For each attribute key in the set of attribute keys to be compared, perform the following operations:

[0112] If the attribute key exists only in the valid attribute set 1, then mark the difference type as "attr_extra", indicating that the first node has redundant attributes. Obtain the weight value of the first node, multiply it by 0.1 as the attribute weight coefficient, and record the difference information, which includes the node path, difference type, and weight value. Set the attribute matching flag (set to true during initialization) to false.

[0113] If the attribute key exists only in the set of valid attributes 2, then mark the difference type as "attr_missing", indicating that the first node is missing an attribute, obtain the weight value of the first node, multiply it by 0.1 as the attribute weight coefficient, record the difference information, and set the attribute matching flag to false;

[0114] If the attribute key exists in two valid attribute sets but the attribute values ​​are different, the difference type is marked as "attr_diff", indicating that the attribute values ​​are different. The weight value of the first node is obtained, multiplied by 0.1 as the attribute weight coefficient, the difference information is recorded, and the attribute matching flag is set to false.

[0115] If the attribute key exists in two valid attribute sets and the attribute values ​​are equal, then the attribute matching flag is set to true.

[0116] S45, compare node values.

[0117] First, initialize the value matching flag to true;

[0118] Then, it is determined whether the value of the first node is equal to the value of the second node. If they are not equal, the difference type is marked as "value_diff", indicating a difference in node values. The weight value of the first node is obtained, and the difference information is recorded. This difference information includes the node path, difference type, the value of the first node, and the value of the second node. The value matching flag is then set to false. If the value of the first node is equal to the value of the second node (including the case where neither has a value), the initial value matching flag is maintained as true.

[0119] S46, Statistics on the matching status of the current node.

[0120] Determine whether both the attribute matching flag and the value matching flag are true. If both are true, add the weight value of the first node to the matching weight variable, indicating that the current node is a complete match, and then proceed to step S47. If either the attribute matching flag or the value matching flag is false, or both are false, then the comparison ends.

[0121] S47, recursively compare child nodes.

[0122] In this embodiment, step S47 includes the following sub-steps:

[0123] S471, Determine whether to apply the ignore order rule.

[0124] Query the rule index to obtain the rule list corresponding to the current node path;

[0125] Check if a rule of type IGNORE_ORDER exists in the rule list. If it exists, ignore the order flag and mark it as true; if it does not exist, it means that no node needs to apply this rule, ignore the order flag and mark it as false.

[0126] S372: Select child node matching strategy.

[0127] Based on the ignored order flag and the number of child nodes, select the appropriate matching strategy:

[0128] Strategy 1: Optimal Matching Strategy

[0129] If any of the following conditions are met:

[0130] The order flag is ignored if it is true.

[0131] The number of child nodes of the first node is not equal to the number of child nodes of the second node;

[0132] The optimal matching strategy is then adopted, and the specific steps are as follows:

[0133] (1) Construct a similarity matrix:

[0134] Create an m×n two-dimensional matrix, where m is the number of child nodes of the first node and n is the number of child nodes of the second node;

[0135] For each element (i, j) in the matrix, calculate the similarity score between the i-th child node of the first node and the j-th child node of the second node. If the names of the two child nodes are the same, the similarity score is 1.0; otherwise, it is 0.0.

[0136] (2) Applying the maximum weight matching algorithm for bipartite graphs:

[0137] The similarity matrix is ​​solved using the Hungarian Algorithm.

[0138] Find the node pairing scheme that maximizes the total similarity score;

[0139] Output a list of matching pairs, each pair containing the child node indices of the first node and the child node indices of the second node;

[0140] (3) Recursively compare matching pairs:

[0141] Initialize the set of matched second node indices to an empty set;

[0142] Iterate through each matching pair in the list and perform the following operations:

[0143] If the second node index of the matching pair is not empty and the similarity score is greater than 0, then the second node index is added to the set of matched second node indexes, and then step S4 is recursively called, that is, step S4 is returned to query the corresponding rule and the two child nodes of the matching pair are compared.

[0144] If the index of the second node of the matching pair is empty, then recursively call step S4, pass in the child node of the first node and the empty node, find the matching rule corresponding to the unmatched child node and perform the matching. If there is no rule, proceed to the next child node until all child nodes are compared.

[0145] (4) Process the unmatched child nodes in the second node:

[0146] Traverse the list of child nodes of the second node. For each index, if the index is not in the set of matched second node indices, recursively call step S4, passing in the empty node and the child node of the second node, and process the unmatched child nodes in the second node.

[0147] Strategy 2: Sequential matching strategy.

[0148] If the conditions of Strategy 1 are not met, i.e., the order flag is ignored and the number of child nodes is equal, then the order matching strategy is adopted:

[0149] Iterate through the list of child nodes of the first and second nodes in order.

[0150] For each pair of child nodes at corresponding positions, step S4 is recursively called for comparison.

[0151] In this embodiment, step S5 includes the following sub-steps:

[0152] S51, the overall similarity is calculated using the following formula: Overall similarity = Matching weight ÷ Total weight;

[0153] Wherein, the matching weight is the value of the matching weight variable, representing the sum of the weights of completely matching nodes; the total weight is the value of the total weight variable, representing the sum of the weights of all nodes; if the total weight is 0 (both files are empty), then the overall similarity is defined as 1.0; the range of the overall similarity is [0, 1], where 0 represents completely different and 1 represents completely identical.

[0154] S52, Calculate structural similarity:

[0155] Filter out the structural type differences from the difference list, which include difference items with difference types of "missing", "extra", and "name_diff";

[0156] The number of differences in statistical structural types is denoted as the structural difference number.

[0157] The structural similarity is calculated using the following formula: M represents the number of structural differences, M1 represents the total number of nodes in the first node tree, and M2 represents the total number of nodes in the second node tree.

[0158] The structural similarity reflects the degree of consistency in the tree structure of the two files to be compared, and the value range is [0,1].

[0159] S53, Calculate content similarity:

[0160] Filter the content type differences from the difference list, including difference items with difference types of "value_diff", "attr_diff", "attr_missing", and "attr_extra".

[0161] The number of differences in content types is recorded as the content difference count.

[0162] The following formula is used to calculate content similarity: N represents the number of structural differences;

[0163] The content similarity reflects the degree of consistency of the content of the matched nodes, and the value range is [0, 1].

[0164] S54 generates a comprehensive comparison report.

[0165] The comprehensive comparison report may include:

[0166] Overall similarity: Indicates the degree of similarity between the files as a whole;

[0167] Structural similarity: Indicates the degree of similarity in file structure;

[0168] Content similarity: Indicates the degree of similarity between the contents of the files;

[0169] Total number of nodes: The larger of the two files to be compared;

[0170] Number of matching nodes: The number of nodes that are completely matched;

[0171] Difference details list: Contains detailed information on all differences, including the node path, difference type, values ​​involved, weights, etc. for each difference.

[0172] Text summary: Human-readable descriptions generated based on overall similarity, such as "High similarity (95.00%), 5 differences found".

[0173] Example of application scenarios:

[0174] A software system needs to compare two different versions of XML configuration files to determine whether the configuration has undergone substantial changes. The configuration files contain non-critical fields such as timestamps and comments, and changes to these fields should not affect the similarity assessment.

[0175] Input file 1 (config_v1.xml):

[0176] <?xml version="1.0" encoding="UTF-8"?>

[0177] <configuration version="1.0" timestamp="2024-01-01T10:00:00">

[0178] <database>

[0179] <host> 192.168.1.100< / host>

[0180] <port> 3306< / port>

[0181] <username> admin< / username>

[0182] <password> secret123< / password>

[0183] < / database>

[0184] <cache>

[0185] <enabled> true< / enabled>

[0186] <ttl> 3600< / ttl>

[0187] < / cache>

[0188] <logging level="INFO" / >

[0189] < / configuration>

[0190] Input file 2 (config_v2.xml):

[0191] <?xml version="1.0" encoding="UTF-8"?>

[0192] <configuration version="1.0" timestamp="2024-01-05T15:30:00">

[0193] <database>

[0194] <host> 192.168.1.100< / host>

[0195] <port> 3306< / port>

[0196] <username> admin< / username>

[0197] <password> secret123< / password>

[0198] < / database>

[0199] <cache>

[0200] <enabled> true< / enabled>

[0201] <ttl> 7200< / ttl>

[0202] < / cache>

[0203] <logging level="DEBUG" / >

[0204] < / configuration>

[0205] Whitelist rule configuration (rules.json):

[0206] {

[0207] "rules": [

[0208] {

[0209] "path": " / configuration",

[0210] "field_name": "timestamp",

[0211] "rule_type": "filter",

[0212] "description": "Ignore timestamp field"

[0213] },

[0214] {

[0215] "path": " / configuration",

[0216] "field_name": "version",

[0217] "rule_type": "equals",

[0218] "value": "1.0",

[0219] "description": "Version number must be 1.0"

[0220] },

[0221] {

[0222] "path": " / configuration / cache",

[0223] "field_name": "ttl",

[0224] "rule_type": "weight",

[0225] "value": 0.5,

[0226] "description": "Cache TTL weight reduced"

[0227] } ]

[0229] }

[0230] Execution steps:

[0231] Step S1: Parse the baseline configuration file and construct a whitelist rule index

[0232] S11: Read the content of the rules.json file;

[0233] S12: Parse the JSON content and extract 3 rules to form a rule list:

[0234] Rule 1: Filter the timestamp attribute of the configuration root node;

[0235] Rule 2: Require the version attribute of the configuration root node to be equal to "1.0";

[0236] Rule 3: Set the weight of the ttl attribute of the cache node to 0.5;

[0237] S13: Construct the rule index:

[0238] Rule index = {

[0239] " / configuration": [Rule 1, Rule 2],

[0240] " / configuration / cache": [Rule 3]

[0241] }

[0242] Step S2: Parse the files to be compared into a node tree structure

[0243] S21: Read the content of the config_v1.xml and config_v2.xml files;

[0244] S22: Identify the format as XML based on the file extension ".xml" and content feature "<?xml";

[0245] S23: Invoke the XML parser to parse file 1 into a first node tree and file 2 into a second node tree:

[0246] First node tree (partial structure):

[0247] Node: / configuration

[0248] Attributes: {version: "1.0", timestamp: "2024-01-01T10:00:00"}

[0249] Child nodes:

[0250] - / configuration / database

[0251] Child nodes:

[0252] - / configuration / database / host (value: "192.168.1.100")

[0253] - / configuration / database / port (value: "3306")

[0254] - / configuration / database / username (value: "admin")

[0255] - / configuration / database / password (value: "secret123")

[0256] - / configuration / cache

[0257] Child nodes:

[0258] - / configuration / cache / enabled (value: "true")

[0259] - / configuration / cache / ttl (value: "3600")

[0260] - / configuration / logging

[0261] Attribute: {level: "INFO"}

[0262] The second node has a similar tree structure, but the difference lies in the TTL value being "7200" and the logging level being "DEBUG".

[0263] Step S3: Initialize the comparison state.

[0264] S31: Initialize statistical variables:

[0265] Total weight = 0;

[0266] Match weight = 0;

[0267] Total number of nodes = 0;

[0268] Difference list = [];

[0269] S32: Add the root node pair to the comparison queue;

[0270] S33: Construct the path mapping table.

[0271] Step S4: Recursively compare nodes.

[0272] First recursion: Compare root node / configuration

[0273] S41: Both nodes exist, so continue the comparison;

[0274] S42: The node name "configuration" and the path " / configuration" are both equal, so continue the comparison;

[0275] S43: Check the EQUALS rules; rule 2 requires version="1.0":

[0276] The first node's version attribute is "1.0", which satisfies...

[0277] The second node's version attribute is "1.0", which satisfies...

[0278] Continue execution

[0279] S44: Compare node attributes:

[0280] S441: Apply the FILTER rule, rule 1 requires filtering the timestamp attribute.

[0281] Remove the timestamp attribute from the attribute sets of both nodes.

[0282] Valid attribute set 1 = {version: "1.0"}

[0283] Valid attribute set 2 = {version: "1.0"}

[0284] S442: Compare valid attributes

[0285] The version attribute value is "1.0" for all of them, matching

[0286] Attribute matching flag = true

[0287] S45: Compare node values:

[0288] Both nodes have no value (non-leaf nodes)

[0289] Value matching flag = true

[0290] S46: Statistics on the matching status of the current node:

[0291] Total weight += 1.0 (default weight of the root node)

[0292] Both attributes and values ​​match, so the match weight is += 1.0.

[0293] S47: Recursive comparison of child nodes:

[0294] S471: Query rule index, no IGNORE_ORDER rule, ignore order flag = false

[0295] S472: All nodes have 3 child nodes, using a sequential matching strategy.

[0296] Recursive comparison / configuration / database

[0297] Recursive comparison of / configuration / cache

[0298] Recursive comparison / configuration / logging

[0299] Second recursion: Compare the / configuration / database node

[0300] (All child nodes under this node have the same values ​​for host, port, username, and password)

[0301] Execute S41-S47, all attributes and values ​​are matched, and the results are accumulated into the matching weight.

[0302] Third recursion: Compare the / configuration / cache node

[0303] Executing S41-S47, the enabled nodes match, but the ttl node values ​​are different:

[0304] 3.1 recursion: Compare the / configuration / cache / ttl node

[0305] S41: Both nodes exist, continue;

[0306] S42: Both node name and path match, continue;

[0307] S43: No EQUALS rules, continue;

[0308] S44: No attributes required for comparison;

[0309] S45: Compare node values:

[0310] The value of the first node is "3600"

[0311] The value of the second node is "7200"

[0312] If the values ​​are not equal, record the difference information:

[0313] Difference list. Add({

[0314] 'path': ' / configuration / cache / ttl',

[0315] 'type': 'value_diff',

[0316] 'value1': '3600',

[0317] 'value2': '7200',

[0318] 'weight': 0.5 # Apply the weights from rule 3

[0319] })

[0320] Value matching flag = false

[0321] S46: Statistics on the matching status of the current node:

[0322] Query the index by rule 3, setting its weight to 0.5.

[0323] Total weight += 0.5

[0324] If the attribute matches but the value does not, the matching weight does not increase.

[0325] Fourth recursion: Compare / configuration / logging nodes

[0326] S44: Compare node attributes:

[0327] S442: Comparison Attributes

[0328] The `level` attribute: the first node is "INFO", the second node is "DEBUG", which is a mismatch.

[0329] Record the differences:

[0330] Difference list. Add({

[0331] 'path': ' / configuration / logging / @level',

[0332] 'type': 'attr_diff',

[0333] 'value1': 'INFO',

[0334] 'value2': 'DEBUG',

[0335] 'weight': 0.1 # Attribute weight coefficient

[0336] })

[0337] Attribute matching flag = false

[0338] S46: Statistics on the matching status of the current node:

[0339] Total weight += 1.0

[0340] If the attributes do not match, the matching weight will not increase.

[0341] Continue recursively to other nodes; detailed process omitted here.

[0342] Step S5: Calculate multi-dimensional similarity

[0343] S51: Calculate overall similarity:

[0344] Assuming that after complete recursive comparison, the total weight = 10.5 (including the weight of all nodes) and the matching weight = 9.0 (most nodes match, but TTL and logging do not match), then the median similarity = 9.0 ÷ 10.5 = 0.857 = 85.7%;

[0345] S52: Calculate structural similarity:

[0346] Both files have a total of 11 nodes. There are no differences in the missing, extra, or name_diff types in the difference list, and the number of structural differences is 0. Therefore, the structural similarity = 1 - (0 ÷ 11) = 1.0 = 100%.

[0347] S53: Calculate content similarity:

[0348] There are two content type differences in the difference list (ttl's value_diff and logging's attr_diff), therefore the content similarity = 1 - (2 ÷ 11) = 0.818 = 81.8%;

[0349] S54: Generate a comprehensive comparison report:

[0350] {

[0351] "similarity": 0.857,

[0352] "structure_similarity": 1.0,

[0353] "content_similarity": 0.818,

[0354] "total_nodes": 11,

[0355] "matched_nodes": 9,

[0356] "differences": [

[0357] {

[0358] "path": " / configuration / cache / ttl",

[0359] "type": "value_diff",

[0360] "value1": "3600",

[0361] "value2": "7200",

[0362] "weight": 0.5

[0363] },

[0364] {

[0365] "path": " / configuration / logging / @level",

[0366] "type": "attr_diff",

[0367] "value1": "INFO",

[0368] "value2": "DEBUG",

[0369] "weight": 0.1

[0370] }

[0371] ],

[0372] Summary: "High similarity (85.70%), with 2 differences found."

[0373] }

[0374] Conclusion: The overall similarity between the two configuration files is 85.7%, with identical structures. The main differences lie in the adjustments to the cache TTL and log level parameters. The timestamp field is filtered out, which does not affect the similarity assessment. Users can use this result to determine the extent of the impact of configuration changes.

[0375] like Figure 3As shown, based on the same inventive concept, this embodiment also provides a structured data file comparison system, including the following components:

[0376] The rule index building module is used to parse the baseline configuration file to extract the whitelist rule list and build a rule index based on the whitelist rule list;

[0377] The node tree generation module is used to parse two files to be compared into a first node tree and a second node tree, respectively.

[0378] The initialization module is used to initialize the alignment state, including initializing the total weight variable and the matching weight variable;

[0379] The recursive comparison module is used to take the nodes at corresponding positions in the first node tree and the second node tree as node pairs, and recursively compare the node pairs based on the rule index.

[0380] The similarity calculation module is used to calculate similarity from multiple dimensions after the comparison is completed and generate a comprehensive comparison report.

[0381] For details on the specific execution operations of each module, please refer to the relevant descriptions in the aforementioned method embodiments, which will not be repeated here.

[0382] like Figure 4 As shown, this embodiment also provides an electronic device that may include a processor and a memory, wherein the memory is coupled to the processor. It is worth noting that this figure is exemplary, and other types of structures can be used to supplement or replace this structure to achieve data extraction, report generation, communication, or other functions.

[0383] like Figure 4 As shown, the electronic device may also include an input unit, a display unit, and a power supply. It is worth noting that the electronic device is not necessarily required to include these components. Figure 4 All components shown in the image. Furthermore, electronic devices may also include... Figure 4 For components not shown, please refer to existing technologies.

[0384] A processor, sometimes also called a controller or operating control, may include a microprocessor or other processor device and / or logic device that receives input and controls the operation of various components of an electronic device.

[0385] The memory may be one or more of the following: a cache, flash memory, hard drive, removable media, volatile memory, non-volatile memory, or other suitable devices. It can store information such as the processor's configuration information and the instructions executed by the processor. The processor can execute programs stored in the memory to perform information storage or processing. In one embodiment, the memory also includes a buffer memory, or buffer, to store intermediate information.

[0386] This invention also provides a computer program product including computer-readable instructions. When the computer-readable instructions are executed in an electronic device, the program product causes the electronic device to perform the operation steps included in the method of this invention.

[0387] This invention also provides a storage medium storing computer-readable instructions that cause an electronic device to perform the operation steps included in the method of this invention.

[0388] Those skilled in the art will recognize that the units and algorithm steps of the various examples described in conjunction with the embodiments disclosed herein can be implemented in electronic hardware, computer software, or a combination of both. To clearly illustrate the interchangeability of hardware and software, the components and steps of the various examples have been generally described in terms of functionality in the foregoing description. Whether these functions are implemented in hardware or software depends on the specific application and design constraints of the technical solution. Those skilled in the art can use different methods to implement the described functions for each specific application, but such implementations should not be considered beyond the scope of this invention.

[0389] If the integrated unit is implemented as a software functional unit and sold or used as an independent product, it can be stored in a computer-readable storage medium. Based on this understanding, the technical solution of the present invention, in essence, or the part that contributes to the prior art, or all or part of the technical solution, can be embodied in the form of a software product. This computer software product is stored in a storage medium and includes several instructions to cause a computer device (which may be a personal computer, server, or network device, etc.) to execute all or part of the steps of the methods described in the various embodiments of the present invention. The aforementioned storage medium includes various media capable of storing program code, such as USB flash drives, portable hard drives, read-only memory (ROM), random access memory (RAM), magnetic disks, or optical disks.

[0390] The above specific embodiments are merely several optional embodiments of the present invention. Based on the technical solutions of the present invention and the relevant teachings of the above embodiments, those skilled in the art can make various alternative improvements and combinations to the above specific embodiments.

Claims

1. A method for comparing structured data files, characterized in that, Includes the following steps: S1, parse the baseline configuration file to extract the whitelist rule list, and build a rule index based on the whitelist rule list; S2, parse the two files to be compared into a first node tree and a second node tree respectively; S3, Initialize the comparison state, including initializing the total weight variable and the matching weight variable; S4, take the nodes at corresponding positions in the first node tree and the second node tree as node pairs, and recursively compare the node pairs based on the rule index; S5: After the comparison is completed, the similarity is calculated from multiple dimensions and a comprehensive comparison report is generated.

2. The method for comparing structured data files according to claim 1, characterized in that, Step S1 includes the following sub-steps: S11, Obtain the baseline configuration file, which defines the whitelist rules required for the current alignment task; S12, parse the baseline configuration file and extract the whitelist rule list; S13, construct a rule index based on the whitelist rule list. The rule index adopts a hash table structure, with the node path as the key and the rule list as the value.

3. The method for comparing structured data files according to claim 1, characterized in that, Step S2 includes the following sub-steps: S21, Obtain the first and second files to be compared; S22, identify the file formats of the first and second files to be compared; S23, according to the identified file format, call the corresponding parser to parse the first file to be compared into a first node tree and the second file to be compared into a second node tree.

4. The method for comparing structured data files according to claim 1, characterized in that, In step S4, for each node pair to be compared, the following sub-steps are performed: S41, determine whether the two nodes in the node pair exist: if neither exists, return directly; if only one node exists, record the difference information and do not continue to recursively compare child nodes, return directly; if both exist, proceed to step S42. S42, determine whether the node names and node paths of the two nodes are equal: if the node names or node paths are not equal, record the difference information, do not continue to recursively compare child nodes, and return directly; if the node names and paths are equal, continue to execute step S43. S43, query the rule index to obtain the rule list corresponding to the current node path, filter out the EQUALS type rules and obtain the expected value from the rules, and for each EQUALS type rule, determine whether the actual value in the attribute set of the two nodes is equal to the expected value. If the actual value of either of the two nodes is not equal to the expected value, record the difference information and do not continue to recursively compare child nodes, and return directly; if the actual value of the two nodes under all EQUALS type rules is equal to the expected value or there is no EQUALS type rule in the rule list, then proceed to step S44. S44: Filter out FILTER type rules from the rule list and extract the field names to be filtered from them. Remove the fields to be filtered from the attribute sets of the two nodes respectively. Then compare whether the attributes of the two nodes are equal and whether the attribute values ​​are equal. If they are equal, the attribute match is marked as true; otherwise, it is marked as false. S45: Determine whether the node values ​​of two nodes are equal. If they are equal, mark the value match as true; otherwise, mark it as false. S46, determine whether the attribute matching flag and the value matching flag are both true. If so, add the weight value of the first node to the matching weight variable and proceed to step S47. Otherwise, end the comparison. The first node is the node in the node pair that originates from the first node tree. S47, recursively compare the child nodes of the current node.

5. The method for comparing structured data files according to claim 4, characterized in that, S47 includes the following sub-steps: Query the rule index to obtain the rule list corresponding to the current node path, and determine whether there is an IGNORE_ORDER type rule in the rule list. If it exists, ignore the order flag and mark it as true; otherwise, mark it as false. If the order flag is ignored or the number of child nodes of the two nodes is not equal, the optimal matching strategy is used for comparison; otherwise, the order matching strategy is used for comparison. The optimal matching strategy is as follows: Create an m×n two-dimensional matrix, where m is the number of child nodes of the first node and n is the number of child nodes of the second node, where the second node is the node in the node pair originating from the second node tree; for each element (i, j) in the matrix, calculate the similarity score between the i-th child node of the first node and the j-th child node of the second node. If the names of the two child nodes are the same, the similarity score is 1.0, otherwise it is 0.0; solve the similarity matrix to find the node pairing scheme that maximizes the total similarity score, and output a list of matching pairs, where each matching pair contains the child node index of the first node and the child node index of the second node; traverse each matching pair in the list of matching pairs. If the second node index of the matching pair is not empty and the similarity score is greater than 0, add the second node index to the set of matched second node indices, and then recursively call step S4 to compare the two child nodes of the matching pair; if the second node index of the matching pair is empty, recursively call step S4, passing in the child nodes of the first node and the empty node, and process the unmatched child nodes in the first node; Iterate through the list of child nodes of the second node. For each index, if the index is not in the set of matched second node indices, recursively call step S4, passing in the empty node and the child node of the second node, and compare the unmatched child nodes in the second node. The sequential matching strategy is as follows: traverse the child node lists of the first and second nodes in sequence, and for each pair of child nodes at corresponding positions, recursively call step S4 for comparison.

6. The method for comparing structured data files according to claim 1, characterized in that, In step S5, the process of calculating similarity from multiple dimensions includes: Calculate overall similarity: Overall similarity = Matching weight ÷ Total weight; Calculate structural similarity: M represents the number of structural type differences, M1 represents the total number of nodes in the first node tree, and M2 represents the total number of nodes in the second node tree; Calculate content similarity: N represents the number of content type differences.

7. A system for comparing structured data files, characterized in that, It includes the following components: The rule index building module is used to parse the baseline configuration file to extract the whitelist rule list and build a rule index based on the whitelist rule list; The node tree generation module is used to parse two files to be compared into a first node tree and a second node tree, respectively. The initialization module is used to initialize the alignment state, including initializing the total weight variable and the matching weight variable; The recursive comparison module is used to take the nodes at corresponding positions in the first node tree and the second node tree as node pairs, and recursively compare the node pairs based on the rule index. The similarity calculation module is used to calculate similarity from multiple dimensions after the comparison is completed and generate a comprehensive comparison report.

8. A computer program product comprising computer-readable instructions, characterized in that, When executed by a processor, the computer-readable instructions implement the steps in the method for comparing structured data files according to any one of claims 1-6.

9. A computer-readable storage medium comprising computer-readable instructions, characterized in that, When executed by a processor, the computer-readable instructions implement the steps in the method for comparing structured data files according to any one of claims 1-6.

10. An electronic device, characterized in that, include: Memory, which stores program instructions; The processor, connected to the memory, executes program instructions in the memory to implement the steps in the method for comparing structured data files according to any one of claims 1-6.