Answer the question
In order to leave comments, you need to log in
Is it possible to update part of an XML file via PHP?
There is a view file (simplified):
<products>
<product id="1">
<name>Test 01</name>
<price>123</price>
</product>
...
<product id="999">
<name>Test 999</name>
<price>456</price>
</product>
</products>
<product id="123">
<product id="123">
<price>456</price>
Answer the question
In order to leave comments, you need to log in
$products = simplexml_load_file('your_file.xml');
$products[0]->price = 321;
$products->asXML('your_file.xml');
If you generate the xml file, you can guarantee, for example, its formatting (prohibit the transfer of data that is critical for you to new lines), in this case, a lot of situations can be handled with a simple search and replace.
In difficult situations, when you need to look for exactly where and what to replace, you can parse xml yourself, line by line, there are even ready-made methods for this
https://secure.php.net/manual/ru/ref.xml.php
i.e. you parse xml line by line to find the desired line, and write as with a regular text file, line by line, replacing the necessary data on the fly.
ps if you have large data that such optimization is required (if the option described in the next answer does not suit you), then ... change the logic of your program, .. xml (or for example json) is generally the most inappropriate tool for storage.
What is the point? It's not a database, it's a text file.
A file that will be completely overwritten with any change.
If you store something in bulky XML, and you are worried about the possible load, then divide them according to some parameters, let your file system have not one large file, but the N number of smaller files.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question