-1

I have a file with products: http://com6.com/items.xml
I want to remove all products priced at less than 200 I have the following code:

<?php



$xml = simplexml_load_file('http://com6.com/items.xml ');
$toDelete = array();
foreach ($xml->PRICE as $PRICE) {
$cena  = $PRICE->RETAIL_PRICE;
if ($cena < 200 ) {$toDelete[] = $PRICE; } 
}



foreach ($toDelete as $PRICE) {
    $dom = dom_import_simplexml($PRICE);
    $dom->parentNode->removeChild($dom);
}


$xml->asXML("result.xml");


?>

This code works with other XML files but with this: http://com6.com/items.xml did not work.

ilia2
  • 11
  • 5

1 Answers1

0

There are times when XML isn't loaded properly and I face this issue all the time. In such cases what you could do is convert the XML to an associative Array and delete that key from the array.

<?php
$xmlObject = simplexml_load_string($xml_URL);
$json = json_encode($xmlObject);
$array = json_decode($json, TRUE);

foreach($array as $element) {
    if($element['RETAIL_PRICE'] < 200) {
        unset($element);
    }
}
vnay92
  • 318
  • 1
  • 4
  • 13
  • How to save the file with result? – ilia2 Dec 13 '15 at 17:04
  • Warning: simplexml_load_string() [function.simplexml-load-string]: Entity: line 1: parser error : Start tag expected, '<' not found in /home/...... – ilia2 Dec 13 '15 at 17:16
  • you could use simplexml_load_string(file_get_contents($URL)); – vnay92 Dec 13 '15 at 17:21
  • no success I wrote via function array_to_xml but in result element['RETAIL_PRICE'] with price < 200 is not removed. To work need javascript? – ilia2 Dec 13 '15 at 17:56