2016-07-10 46 views
1

我想比较和检查两个xml字符串之间的差异,但我的代码没有检测到任何更改的xml字符串! 为前我的第一个字符串包含:使用php的两个xml字符串之间的差异

  <Result> 
       <pid>10</pid> 
       <DocID>29</DocID> 
       <Response>True</Response> 
       <DocID>60<DocID> 
       <Blvd_Name>dfdfdfdfd</Blvd_Name> 
       <Alley_Name>dfd</Alley_Name> 
       <Plate_Number>654654</Plate_Number> 
       <Post_Code>654654654</Post_Code> 
       <Phone_1>654654</Phone_1> 
       <Phone_2>654654564</Phone_2> 
       <Fax>2323232</Fax> 
       <Website>ewewew</Website> 
       <Mobile_No>23232323232</Mobile_No> 
       <Information> 
        <Info> 
         <National_Code>106397854</National_Code> 
         <Start_Activity_Date>2015-12-22 00:00:00</Start_Activity_Date> 
         <End_Activity_Date>2016-01-03 00:00:00</End_Activity_Date> 
        </Info> 
       </Information> 
       <Service_Times> 
        <Service_Time>15:30 - 17:45</Service_Time> 
       </Service_Times> 
      </Result> 

第二个字符串是:

   <Result> 
       <pid>10</pid> 
       <DocID>29</DocID> 
       <Response>True</Response> 
       <DocID>60<DocID> 
       <Blvd_Name>dfdfdfdfd</Blvd_Name> 
       <Alley_Name>dfd</Alley_Name> 
       <Plate_Number>654654</Plate_Number> 
       <Post_Code>654654654</Post_Code> 
       <Phone_1>11111</Phone_1> 
       <Phone_2>6546111154564</Phone_2> 
       <Fax>11111</Fax> 
       <Website>11111</Website> 
       <Mobile_No>11111</Mobile_No> 
       <Information> 
        <Info> 
         <National_Code>106397854</National_Code> 
         <Start_Activity_Date>2015-12-22 8:01:50</Start_Activity_Date> 
         <End_Activity_Date>2016-01-03 11:20:10</End_Activity_Date> 
        </Info> 
       </Information> 
       <Service_Times> 
        <Service_Time>15:30 - 17:45</Service_Time> 
       </Service_Times> 
      </Result> 

你可以看到有在对象的值一定的差异! 我试过simplexmlload,然后array_diff和杰森编码和解码和比较杰森,但没有机会来检测差异。 任何建议如何做到这一点?

我的数组DIFF代码:

$result = array_diff($Data1, $Data2); 

     if(empty($result)){ 
      // the XML documents are the same 
      $res = "No changes"; 
     } else { 
      // they are different 
      $res = "There are Some changes"; 
     } 
+0

检查可能只做价值或结构检查可以比较吗? – splash58

+0

@ splash58结构也很重要,例如在第二个字符串中它可能不包含某个对象。 – anonymox

+0

你的xml格式不正确(有错误) – RomanPerekhrest

回答

0

您可以将数据作为原始文本ANE通过以下

<?php 
$difference = xdiff_string_diff($Data1, $Data2); 
0

好吧,我解决了,如果比较方法以及使用简单的问题,看到了差距有效。

我第一次打开两个xml文件,然后我使用下面的方法同步它们,如果我在第二个xml文件中更改值/结构,它会给我“有一些更改”。

$file = './Result.xml'; 
     if (file_exists($file)) { 
      $Data = file_get_contents($file); 
     } else { 
      exit('Failed to open ' . $file); 
     } 

     $file2 = './Result2.xml'; 
     if (file_exists($file2)) { 
      $Data2 = file_get_contents($file2); 
     } else { 
      exit('Failed to open ' . $file2); 
     } 


     if ($Data === $Data2) { 
      // the XML documents are the same 
      $res = "No changes"; 
     } else { 
      // they are different: print the reason why 
      $res = "There are Some changes"; 

     } 
相关问题