使用php的两个xml字符串之间的差异

时间:2016-07-10 06:33:31

标签: php arrays xml string difference

我正在尝试比较并检查两个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";
        }

2 个答案:

答案 0 :(得分:0)

您可以将数据保留为原始文本,并使用以下

查看差异
<?php
$difference = xdiff_string_diff($Data1, $Data2);

答案 1 :(得分:0)

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

我首先打开两个xml文件然后使用下面的方法复制它们,如果我在第二个xml文件中更改了值/结构,它会给我&#34;有一些更改&#34;。

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

        }
相关问题