比较具有相同结构的两个XML文件并找出差异

时间:2022-02-22 17:17:52

I have two XML files. The structure of both XML files is as below:

我有两个XML文件。两个XML文件的结构如下:

<file1>
       <table>
              <name>...</name>
              <columns>
                       <col>
                             <name>...</name>
                             <type>...</type>
                             <fkey>...</fkey>
                       </col>
                       <col>
                             <name>...</name>
                             <type>...</type>
                             <fkey>...</fley>
                       </col>
               <columns>
       </table>
      <table>
              <name>...</name>
              <columns>
                       <col>
                             <name>...</name>
                             <type>...</type>
                             <fkey>...</fkey>
                       </col>
                       <col>
                             <name>...</name>
                             <type>...</type>
                             <fkey>...</fley>
                       </col>
               <columns>
       </table>
 </file1>

Both the XML files will have same tables, but the number of columns can differ. What I am trying to do is compare each column of each table and find the difference(if any) in type and fkey. Also I would like to know which columns are missing from second XML file. And I want to save this info to another file(it can be any format).

两个XML文件都具有相同的表,但列数可能不同。我要做的是比较每个表的每一列,找出类型和fkey的差异(如果有的话)。另外,我想知道第二个XML文件中缺少哪些列。我想将此信息保存到另一个文件(它可以是任何格式)。

I know Perl,PHP and JavaScript but I haven't worked with XML before.

我知道Perl,PHP和JavaScript,但之前我还没有使用过XML。

Now what I want to know is where to start and which tools to use? What would be the best way to do above task? Is there any module which provide functions to perform diff between two XML files?

现在我想知道的是从哪里开始以及使用哪些工具?做上述任务的最佳方法是什么?是否有任何模块提供在两个XML文件之间执行diff的函数?

EDIT: I am working on Linux platform(RedHat).

编辑:我正在Linux平台(RedHat)上工作。

2 个解决方案

#1


1  

For a Perl solution take a look at the related CPAN modules. XML::Diff is the most obvious and seems to do what you need, but there are several to try if that doesn't work for you.

对于Perl解决方案,请查看相关的CPAN模块。 XML :: Diff是最明显的,似乎可以满足您的需求,但如果这对您不起作用,还有几种可以尝试。

#2


2  

I once wrote a module that does something similar (not optomized but it did do the trick for me)

我曾经写过一个类似的模块(没有选择,但它确实为我做了伎俩)

http://ekawas.blogspot.ca/2008/11/comparing-xml-documents-semantically.html

  use XML::SemanticCompare;
  my $x = XML::SemanticCompare->new;

  # compare 2 different files
  my $isSame = $x->compare($control_xml, $test_xml);
  # are they the same
  print "XML matches!\n"
    if $isSame;
  print "XML files are semantically different!\n"
    unless $isSame;

  # get the diffs
  my $diffs_arrayref = $x->diff($control_xml, $test_xml);

#1


1  

For a Perl solution take a look at the related CPAN modules. XML::Diff is the most obvious and seems to do what you need, but there are several to try if that doesn't work for you.

对于Perl解决方案,请查看相关的CPAN模块。 XML :: Diff是最明显的,似乎可以满足您的需求,但如果这对您不起作用,还有几种可以尝试。

#2


2  

I once wrote a module that does something similar (not optomized but it did do the trick for me)

我曾经写过一个类似的模块(没有选择,但它确实为我做了伎俩)

http://ekawas.blogspot.ca/2008/11/comparing-xml-documents-semantically.html

  use XML::SemanticCompare;
  my $x = XML::SemanticCompare->new;

  # compare 2 different files
  my $isSame = $x->compare($control_xml, $test_xml);
  # are they the same
  print "XML matches!\n"
    if $isSame;
  print "XML files are semantically different!\n"
    unless $isSame;

  # get the diffs
  my $diffs_arrayref = $x->diff($control_xml, $test_xml);