i have an xml file with the following structure.
<?xml version="1.0" encoding="utf-8"?>
<products>
<product>
<nr>0</nr>
<product_id>17856</product_id>
<product_number>54586</product_number>
<product_name>just an name</product_name>
<product_url>product url</product_url>
<price>66.3445</price>
<description> bla die bla </description>
<manufacturer>manu 1</manufacturer>
<category>category 1</category>
<stock>3</stock>
<eancode></eancode>
<date_added>2011-04-18 12:10:28</date_added>
<image_front></image_front>
<image_back></image_back>
<vat_value>1.19</vat_value>
</product>
</products>
The xml file has about 1000 products.
What i would like to achieve is that when i download an new xml PHP code should check which products have changed values of the price an stock.
I have an array recursive method which show the differences between the xml files. But it only shows the xml nodes, i.e. if the stock have been changed i only see the stock in the array.
i use the following function to show the differences.
public function checkFile($file){
$xml = simplexml_load_file("./var/import/".$file); //nieuw
$xml1 = $this->simplexml2array($xml);
$xmls = simplexml_load_file("./var/import/oud/".$file); //oud
$xml2 =$this->simplexml2array($xmls);
$checkdiff = $this->arrayRecursiveDiff($xml2, $xml1);
if ($checkdiff == NULL){
return false;
}else{
return true;
}
}
public function arrayRecursiveDiff($aArray1, $aArray2) {
$aReturn = array();
foreach ($aArray1 as $mKey => $mValue) {
if (array_key_exists($mKey, $aArray2)) {
if (is_array($mValue)) {
$aRecursiveDiff = $this->arrayRecursiveDiff($mValue, $aArray2[$mKey]);
if (count($aRecursiveDiff)) { $aReturn[$mKey] = $aRecursiveDiff; }
} else {
if ($mValue != $aArray2[$mKey]) {
$aReturn[$mKey] = $mValue;
}
}
} else {
$aReturn[$mKey] = $mValue;
}
}
return $aReturn;
}
function simplexml2array($xml) {
if (get_class($xml) == 'SimpleXMLElement') {
$attributes = $xml->attributes();
foreach($attributes as $k=>$v) {
if ($v) $a[$k] = (string) $v;
}
$x = $xml;
$xml = get_object_vars($xml);
}
if (is_array($xml)) {
if (count($xml) == 0) return (string) $x; // for CDATA
foreach($xml as $key=>$value) {
$r[$key] = $this->simplexml2array($value);
}
if (isset($a)) $r['@attributes'] = $a; // Attributes
return $r;
}
return (string) $xml;
}
can anybody help me not to only show the the difference field of the xml but the whole xml product information which have been changed.
thank you all in advance.