By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
431,663 Members | 762 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 431,663 IT Pros & Developers. It's quick & easy.

Update only Xml node (PHP5)

P: n/a
Hi all

can I update a xml node AND write the new value in a file witout
re-write all the file ?

sample :

my xml file :

<sample>
<exo id="1" value="Test" />
</sample>

i want to modify the file like

<sample>
<exo id="1" value="Test updated" />
</sample>

is it possible ?

Jul 17 '06 #1
Share this Question
Share on Google+
5 Replies


P: n/a
An XML file is like an HTML file or TXT file. Normally using the Dom
XML you cannot read and update part of an XML file directly off disk.
You must read the whole file in memory, change part of it in memory and
then you must rewrite the whole file back to disk.

However there are database engines that do this (I've read about them
but never used them). But your XML file will have a lot of extra
attributes used by the database engine.

It depends what you are doing.

jh****@gmail.com wrote:
Hi all

can I update a xml node AND write the new value in a file witout
re-write all the file ?

sample :

my xml file :

<sample>
<exo id="1" value="Test" />
</sample>

i want to modify the file like

<sample>
<exo id="1" value="Test updated" />
</sample>

is it possible ?
Jul 17 '06 #2

P: n/a

jh****@gmail.com wrote:
Hi all

can I update a xml node AND write the new value in a file witout
re-write all the file ?

sample :

my xml file :

<sample>
<exo id="1" value="Test" />
</sample>

i want to modify the file like

<sample>
<exo id="1" value="Test updated" />
</sample>

is it possible ?
No. Think about it: Where are those extra 8 bytes coming from? A
file is a contiguous piece of data, you can't insert into it randomly,
it has to be reallocated. (if you wanted to get really technically, it
could potentially be possible to create a small piece and jumping
around using i nodes, but thats way beyond the scope of our discussion)

Jul 17 '06 #3

P: n/a
You will have to read the whole file to perform this task, but if your
using PHP5 this can be done in 1 step, using the get
file_get_contents() method to read the xml document into a string.

The following code will make the value change, and it isn't much
trouble to do...

<?php

# Set Values
$my_file = 'myfile.xml';
$new_value = 'Test updated';

# Get file contents
$xml = file_get_contents($my_file);

# Regex
$xml = preg_replace('<exo id="(.*)" value="(.*)" />', "exo id=\"$1\"
value=\"$new_value\" /", $xml);

# Write file out again
file_put_contents($my_file, $xml);

?>
jh****@gmail.com wrote:
Hi all

can I update a xml node AND write the new value in a file witout
re-write all the file ?

sample :

my xml file :

<sample>
<exo id="1" value="Test" />
</sample>

i want to modify the file like

<sample>
<exo id="1" value="Test updated" />
</sample>

is it possible ?
Jul 18 '06 #4

P: n/a
ok, it's works...

thanks for your help.
Noodle wrote:
You will have to read the whole file to perform this task, but if your
using PHP5 this can be done in 1 step, using the get
file_get_contents() method to read the xml document into a string.

The following code will make the value change, and it isn't much
trouble to do...

<?php

# Set Values
$my_file = 'myfile.xml';
$new_value = 'Test updated';

# Get file contents
$xml = file_get_contents($my_file);

# Regex
$xml = preg_replace('<exo id="(.*)" value="(.*)" />', "exo id=\"$1\"
value=\"$new_value\" /", $xml);

# Write file out again
file_put_contents($my_file, $xml);

?>
jh****@gmail.com wrote:
Hi all

can I update a xml node AND write the new value in a file witout
re-write all the file ?

sample :

my xml file :

<sample>
<exo id="1" value="Test" />
</sample>

i want to modify the file like

<sample>
<exo id="1" value="Test updated" />
</sample>

is it possible ?
Jul 18 '06 #5

P: n/a
Since this is XML you may want to consider using PHP's Dom class (not
Dom XML) to do the updating.

Assuming you have this file: book.xml

<sample>
<exo id="1" value="Test" />
<exo id="2" value="Test2" />
</sample>

This example code will search for all the tags with 'exo' and change
the one whose id=='1'. It will then write back the file.

<?php
$dom=new DOMDocument();
$dom->load('book.xml');
$dom->formatOutput = true;
echo $dom->saveXML(); // show before file
$allnodes = $dom->getElementsByTagName('exo');
foreach ($allnodes as $node) {
if ($node->nodeName=='exo' and $node->getAttribute('id')=='1') {
$val=$node->getAttribute('value') . ' updated';
$node->setAttribute ( 'value', $val );
}
}
echo $dom->saveXML(); //show after file
$dom->save('book.xml');
exit;
?>

jh****@gmail.com wrote:
ok, it's works...

thanks for your help.
Noodle wrote:
You will have to read the whole file to perform this task, but if your
using PHP5 this can be done in 1 step, using the get
file_get_contents() method to read the xml document into a string.

The following code will make the value change, and it isn't much
trouble to do...

<?php

# Set Values
$my_file = 'myfile.xml';
$new_value = 'Test updated';

# Get file contents
$xml = file_get_contents($my_file);

# Regex
$xml = preg_replace('<exo id="(.*)" value="(.*)" />', "exo id=\"$1\"
value=\"$new_value\" /", $xml);

# Write file out again
file_put_contents($my_file, $xml);

?>
jh****@gmail.com wrote:
Hi all
>
can I update a xml node AND write the new value in a file witout
re-write all the file ?
>
sample :
>
my xml file :
>
<sample>
<exo id="1" value="Test" />
</sample>
>
i want to modify the file like
>
<sample>
<exo id="1" value="Test updated" />
</sample>
>
is it possible ?
Jul 18 '06 #6

This discussion thread is closed

Replies have been disabled for this discussion.