【问题标题】:Differences between two xml strings using php使用php的两个xml字符串之间的差异
【发布时间】:2016-07-10 06:33:31
【问题描述】:

我正在尝试比较和检查两个 xml 字符串之间的差异,但我的代码未检测到 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 和 jason 编码和解码并比较了 jason,但没有机会检测到差异。 任何建议如何做到这一点?

我的数组差异代码:

 $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";
        }

【问题讨论】:

  • 检查只能对值或结构进行比较?
  • @splash58 结构也很重要,例如在第二个字符串中它可能不包含一些对象。
  • 您的 xml 格式不正确(有错误)
  • @splash58 我已经更新了 xml 字符串,你可以检查一下吗?

标签: php arrays xml string difference


【解决方案1】:

您可以将数据保留为原始文本,并通过以下方式查看差异

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

【讨论】:

    【解决方案2】:

    好的,我使用简单的 if 比较方法解决了这个问题,并且成功了。

    我首先打开了两个 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";
    
            }
    

    【讨论】:

      猜你喜欢
      • 2010-11-28
      • 2012-03-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多