Сравнение 2 XML-файлов с использованием PHP - PullRequest
2 голосов
/ 02 декабря 2010

Я хочу сравнить 2 больших XML-файла и получить различия.Как ExamXML и DiffDog.Решение, которое я нашел, состояло в том, чтобы циклически проходить по всем дочерним узлам каждого файла одновременно и проверять, равны ли они.Но я понятия не имею, как этого добиться ... Как я могу просмотреть все дочерние узлы и их свойства?Как я могу проверить, равен ли первый элемент первого файла первому элементу второго файла, равен ли второй элемент первого файла второму элементу второго файла и т. Д.?

Есть ли у вас идея лучше сравнить 2 xml файла?

Ответы [ 5 ]

2 голосов
/ 02 декабря 2010

Вы когда-нибудь рассматривали использование XPath? Похоже, простой способ захватить все дочерние узлы. Тогда вы сможете пройтись по узлам и сравнить атрибуты / textContent.

2 голосов
/ 26 августа 2011

Я искал что-то для сравнения двух XML, подобных вам, и нашел это решение, которое очень хорошо работает.

http://www.jevon.org/wiki/Comparing_Two_SimpleXML_Documents

Надеюсь, это кому-нибудь поможет.

2 голосов
/ 02 декабря 2010

Это может быть очень альтернативное решение для вас, но я бы так и сделал.

Во-первых, я бы попытался преобразовать формат во что-то гораздо более управляемое, например, в массив, чтобы преобразовать XML в массив.

http://www.bytemycode.com/snippets/snippet/445/

Это простой код для этого.

Тогда в PHP есть функция array_diff (), которая может показать вам различия.

http://www.php.net/manual/en/function.array-diff.php

Это может работать, а может и не работать для вас, учитывая, что вам нужно делать с различиями, но если вы хотите просто определить их и принять меры, это может быть очень быстрым решением вашей проблемы.

1 голос
/ 10 октября 2013

Попробуйте расширение xmldiff

http://pecl.php.net/xmldiff

Оно основано на той же библиотеке, что и модуль perl DifferenceMarkup, вы получите документ diff XML и даже сможете объединиться.*

0 голосов
/ 01 октября 2015
//Child by Child XML files comparison in PHP  
//Returns an array of non matched children in variable &$reasons

$reasons = array();
$xml1 = new SimpleXMLElement(file_get_contents($xmlFile1));
$xml2 = new SimpleXMLElement(file_get_contents($xmlFile2));
$result = XMLFileComparison($xml1, $xml2, $reasons);

/**
 * XMLFileComparison
 * Discription :- This function compares XML files. Returns array
 * of nodes do not match in pass by reference parameter
 * @param $xml1 Object Node Object
 * @param $xml2 Object Node Object
 * @param &$reasons Array  pass by reference 
 * returns array of nodes do not match
 * @param $strict_comparison Bool  default False
 * @return bool <b>TRUE</b> on success or array of strings on failure.
 */
function XMLFileComparison(SimpleXMLElement $xml1, SimpleXMLElement   $xml2, &$reasons, $strict_comparison = false)        
{
    static $str;  
    // compare text content
    if ($strict_comparison) {
        if ("$xml1" != "$xml2") return "Values are not equal (strict)";
    } else {
        if (trim("$xml1") != trim("$xml2"))
                {
                    return " Values are not equal";
                }
    }


    // get all children
    $XML1ChildArray = array();
    $XML2ChildArray = array();
    foreach ($xml1->children() as $b) {
        if (!isset($XML1ChildArray[$b->getName()]))
            $XML1ChildArray[$b->getName()] = array();
        $XML1ChildArray[$b->getName()][] = $b;
    }
    foreach ($xml2->children() as $b) {
        if (!isset($XML2ChildArray[$b->getName()]))
            $XML2ChildArray[$b->getName()] = array();
        $XML2ChildArray[$b->getName()][] = $b;
    }
    //print_r($XML1ChildArray);
    //print_r($XML2ChildArray);
    // cycle over children
    if (count($XML1ChildArray) != count($XML2ChildArray)) return "mismatched children count";// Second File has less or more children names (we don't have to search through Second File's children too)
    foreach ($XML1ChildArray as $child_name => $children) {
        if (!isset($XML2ChildArray[$child_name])) return "Second file does not have child $child_name"; // Second file has none of this child
        if (count($XML1ChildArray[$child_name]) != count($XML2ChildArray[$child_name])) return "mismatched $child_name children count"; // Second file has less or more children

                print_r($child_name);
                foreach ($children as $child) {
            // do any of search2 children match?
            $found_match = false;
            //$reasons = array();
            foreach ($XML2ChildArray[$child_name] as $id => $second_child) {
                            $str = $str.$child_name.($id+1)."/"; // Adding 1 to $id to match with XML data nodes numbers
                            //print_r($child, $second_child);
                            // recursive function call until reach to the end of node
                if (($r = XMLFileComparison($child, $second_child, $reasons, $strict_comparison)) === true) {
                    // found a match: delete second
                    $found_match = true;
                    unset($XML2ChildArray[$child_name][$id]);
                                        $str = str_replace($child_name.($id+1)."/", "", $str);
                                        break;
                } 
                                else {
                                    unset($XML2ChildArray[$child_name][$id]);
                                    $reasons[$str] = $r;
                                    $str = str_replace($child_name.($id+1)."/", "", $str);
                                    break;
                }
            }

        }
    }
  return True;      
}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...