suppose I have an xml like this:
<products>
<product id="1">
<name>aaa</name>
<producturl>aaa</producturl>
<bigimage>aaa</bigimage>
<description>aaa</description>
<price>aaa</price>
<categoryid1>aaa</categoryid1>
<instock>aaa</instock>
</product>
<product id="2">
<name>aaa</name>
<producturl>aaa</producturl>
<bigimage>aaa</bigimage>
<description>aaa</description>
<price>aaa</price>
<categoryid1>aaa</categoryid1>
<instock>aaa</instock>
</product>
</products>
and I need to delete certain node depending on the id attribute, if this attribute is in an array.
I've tried different ways, but the xml is outputted always as the original one!
My code so far:
<?php header("Content-type: text/xml");
$url="http://www.aaa.it/aaa.xml";
$url=file_get_contents($url);
$array=array("1","4","5");
$doc=new SimpleXMLElement($url);
foreach($doc->product as $product){
if(!in_array($product['id'],$array)){
$dom=dom_import_simplexml($product);
$dom->parentNode->removeChild($dom);
// unset($doc->product->$product);
}
}
echo $doc->asXml(); ?>
Thanks a lot everyone.