Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/algorithm/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
设计一个数据结构来比较两个XML文档_Xml_Algorithm_Data Structures_Recursion_Tree - Fatal编程技术网

设计一个数据结构来比较两个XML文档

设计一个数据结构来比较两个XML文档,xml,algorithm,data-structures,recursion,tree,Xml,Algorithm,Data Structures,Recursion,Tree,给出了两个XML文档。提出一个数据结构和代码来比较两个文档,并打印文档之间的差异。就像一个标签存在于一个而不是另一个,或者标签相同但数据不同,等等 我的方法是:使用N元树。N取决于XML文件中属性的数量。现在函数类似于: Bool IsIdentical(tree1,tree2) { if(tree1 == NULL && tree2 == NULL)return true; if(tree1 || tree2) return false; if(tree1-

给出了两个XML文档。提出一个数据结构和代码来比较两个文档,并打印文档之间的差异。就像一个标签存在于一个而不是另一个,或者标签相同但数据不同,等等

我的方法是:使用N元树。N取决于XML文件中属性的数量。现在函数类似于:

Bool IsIdentical(tree1,tree2)
{
   if(tree1 == NULL && tree2 == NULL)return true;
   if(tree1 || tree2) return false;
    if(tree1->data == tree2->data )return true;
   else return (Isindentical(tree1->firstchild,tree2->firstchild) &&  Isindentical(tree1->secondchild,tree2->secondchild) && ........ Isindentical(tree1->nthchild,tree2->nthchild) )

}
你能告诉我我的方法是正确的还是有其他数据结构可以比较吗? 如果我的方法是正确的,那么请告诉我如何产生差异


提前感谢

您的方法是正确的。另一种不需要将两个xml文档同时保存在内存中的方法是为每个文档构造一个xml文档,并对它们进行比较

比较差异是一个更开放的问题,取决于您如何定义文档之间的“差异”。例如。。。vs,可以说这是唯一的共同元素,或者两者都是共同的

最保守的,因此也是最简单的方法是,每当一个节点的任何子节点不同时,声明一个差异,并在此点停止递归,即生成两棵树的交点。实现可能如下所示:

class Tree:
    def __init__(self, value, children):
        self.value = value
        self.children = children

def intersect(a, b):
    if a.value != b.value:
        return None
    children = [x for x in itertools.imap(intersect, a.children, b.children) if x is not None]
    return Tree(a.value, children)
请注意,此解决方案可能过于保守,具体取决于您的目标