0

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

8
  • This may seem very hacky, but why not just open it as a text file and do a string replacement from <STOCK>In Stock</STOCK> to <STOCK>instock</STOCK>. Commented Oct 2, 2019 at 10:52
  • Other than that, this line is likely the issue: $PRODUCT->STOCK[$i]="instock";. I think it should not be using [$i]: $PRODUCT->STOCK = "intstock"; Commented Oct 2, 2019 at 10:53
  • I am attempting to automate everything, the XML will be dropped into my web server by my supplier, and the PHP will cron on a cron job to update the file, and then another cron job will import it into my system and update the stock levels on the products. Thanks for your suggestion - I will give it a go. Commented Oct 2, 2019 at 10:58
  • Unfortunately, that didn't work. Fatal error: Uncaught Error: Call to a member function children() on bool in /updatestock.php:9 Stack trace: #0 {main} thrown in /updatestock.php on line 9 Commented Oct 2, 2019 at 11:02
  • In that case, use $PRODUCT->STOCK[0] = "instock";. See if that does the trick Commented Oct 2, 2019 at 11:11

2 Answers 2

1

This is just because of a slight error with the way you are assigning the new values to your STOCK item:

$PRODUCT->STOCK[$i]="instock";

You have set $i as an increasing number from 0 (on the first iteration). This will set the nth STOCK element to be instock. However, there are no STOCK elements higher than 0 because STOCK is the only child of PRODUCT in each case.

The reason you were getting extra STOCK elements was because they were being created when $i was higher than 0 (STOCK[1] = "instock", STOCK[2] = "instock", etc.).

Simply remove $i completely and use 0 and it will work:

$PRODUCT->STOCK[0]="instock";

Output:

<?xml version="1.0" encoding="UTF-8"?>
<STOREITEMS>
    <PRODUCT ITEM="1234" NAME="Product Number One">
        <STOCK>instock</STOCK>
    </PRODUCT>
    <PRODUCT ITEM="2345" NAME="Product Number Two">
        <STOCK>instock</STOCK>
    </PRODUCT>
    <PRODUCT ITEM="3456" NAME="Product Number Three">
        <STOCK>instock</STOCK>
    </PRODUCT>
</STOREITEMS>
Sign up to request clarification or add additional context in comments.

Comments

0

This is the proper way to do it. I have tested the code my self and its working.

$xml=simplexml_load_file("stock.xml") or die("Error: Cannot create object");
foreach($xml->children() as $data) {         
 echo $data->STOCK . "<br>"; 

if($data->STOCK =='In Stock'){
$data->STOCK='instock';
//htmlentities($xml->save('stock.xml'));
file_put_contents("stock.xml",$xml->saveXML());

}    
} 

echo "success";

let me know if you still have issues..

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.