I hope someone from this amazing community can help me. I'm not brilliant when it comes to PHP, I found a script online to update an XML file - and it kind of works, in the sense that it does the first element in the xml file and then messes the rest of the file up.
My supplier sends me an XML file with updated stock levels, but I need to change the text inside the file.
stock.xml
<?xml version="1.0" encoding="UTF-8"?>
<STOREITEMS>
<PRODUCT ITEM="1234" NAME="Product Number One">
<STOCK>In Stock</STOCK>
</PRODUCT>
<PRODUCT ITEM="2345" NAME="Product Number Two">
<STOCK>In Stock</STOCK>
</PRODUCT>
<PRODUCT ITEM="3456" NAME="Product Number Three">
<STOCK>In Stock</STOCK>
</PRODUCT>
</STOREITEMS>
I need to change In Stock to instock Here's the PHP code I found to do it:
updatestock.php
<?php
$xml1=simplexml_load_file('stock.xml');
$i=-1;
foreach($xml1->children() as $PRODUCT)
{
$i=$i+1;
$STOCK=$PRODUCT->STOCK;
if($STOCK=="In Stock")
{
$PRODUCT->STOCK[$i]="instock";
}
}
file_put_contents("stock.xml",$xml1->saveXML());
?>
I end up with this result:
stock.xml
<?xml version="1.0" encoding="UTF-8"?>
<STOREITEMS>
<PRODUCT ITEM="1234" NAME="Product Number One">
<STOCK>In Stock</STOCK>
</PRODUCT>
<PRODUCT ITEM="2345" NAME="Product Number Two">
<STOCK>In Stock</STOCK><STOCK>instock</STOCK>
</PRODUCT>
<PRODUCT ITEM="3456" NAME="Product Number Three">
<STOCK>In Stock</STOCK><STOCK>instock</STOCK>
</PRODUCT>
</STOREITEMS>
On the 2nd and 3rd products, instead of updating the stock text it just added a new attribute called stock again and entered the text - and it does that all the way through the rest of the file.
If anyone can help me I would really appreciate it. Thank you everyone! Jon
<STOCK>In Stock</STOCK>to<STOCK>instock</STOCK>.$PRODUCT->STOCK[$i]="instock";. I think it should not be using[$i]:$PRODUCT->STOCK = "intstock";$PRODUCT->STOCK[0] = "instock";. See if that does the trick