2016-07-10 44 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"; 

     } 
相關問題