比较两个XML文件而无需考虑元素和属性的顺序 [英] Compare two XML File without care about orders of elements and attributes

查看:191
本文介绍了比较两个XML文件而无需考虑元素和属性的顺序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近遇到了一个问题:我有两个XML文件,我需要检查它们的内容是否相等。这两个文件具有相同类型的元素节点,但是顺序不同,并且节点的属性也相同。举个例子:

I had recently an issue: i have two XML files and i need to check if they are equal for the content. Both file have the same kind of element nodes but in a different order, and the same is for the attributes of the nodes. Take this example:

这是file1.xml

This is file1.xml

<Car name="Ferrari" speed="420">
    <Engine>V12</Engine>
    <Color name="Red"/>
</Car>
<Car name="Lamborghini" speed="380">
    <Engine>SV</Engine>
    <Color name="White"/>
</Car>

这是file2.xml

This is file2.xml

<Car speed="380" name="Lamborghini">
    <Color name="White"/>
    <Engine>SV</Engine>
</Car>
<Car speed="420" name="Ferrari">
    <Color name="Red"/>
    <Engine>V12</Engine>
</Car>

我需要比较这两个文件并在等于时返回true的东西,否则显示弥补差异。 (在示例中,它必须返回true)

I need something that compares this two files and return true if they are "equals", otherwise it shows up the differences. (In the example it must return true)

显然,这是一个示例,我必须检查的文件中包含50.000+行元素。

Obviously this was an example, the files i have to check have like 50.000+ lines of elements inside.

我正在寻找的是一切:软件,要使用的库,手动算法。

What i'm looking for is everything: software, library to use, manual algorithms.

谢谢

推荐答案

首先,我将样本包装到< R>中。 ...< / R> 用它们制成XML文档。

First, I wrapped your samples into <R> ... </R> to make XML documents from them.

然后,我用 xsh 将输入文件处理为元素的规范顺序:我按名称及其@name属性对所有子元素进行了排序。

Then, I used xsh to process the input files into canonical order of elements: I sorted all child elements by name and by their @name attribute.

my $F1 := open file1.xml ;
my $F2 := open file2.xml ;
my $nodes = ( $F1//* | $F2//* ) ;
for my $element in { reverse @$nodes } {
    if ($element/*) {
        xmove &{ sort :k concat(name(), '|', @name) $element/* }
            append $element ;
    }
}

save :f file1.out.xml $F1 ;
save :f file2.out.xml $F2 ;

以相反的顺序遍历节点至关重要,因为否则排序将无法进行。

It's crucial to walk the nodes in reversed order, because otherwise the sorting wouldn't work.

为了比较生成的XML,我使用了旧的xmldiff bash脚本,该脚本使用 xmllint

To compare the resulting XMLs, I used my old xmldiff bash script that uses xmllint:

#!/bin/bash

a=($@)
b=$#
f2=${a[$((--b))]}
f1=${a[$((--b))]}
diff "${a[@]:0:$b}" \
   <(xmllint --c14n "$f1" |xmllint --format -) \
   <(xmllint --c14n "$f2" |xmllint --format -)

这篇关于比较两个XML文件而无需考虑元素和属性的顺序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆