473,322 Members | 1,610 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

Update only Xml node (PHP5)

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
5 5223
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

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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Philipp Lenssen | last post by:
I'm looking for a way to grab *all* XML of a DOM node (in PHP5). Something like the following, only it doesn't work! Thanks. /* I want everything (all XHTML) in <div id="hell=">...</div> */ ...
3
by: Ray | last post by:
I am having my first experience using BLOB as a row in a table. I am using it to insert graphics for labels we print. I have no problem inserting into and select from the table. The graphic is...
1
by: Christian Rühl | last post by:
hey! what i wanna do sounds very simple at first, but it turned out to be a real bone crusher... i want to check if a treeView node is checked and if a correspondent node in my xml config file...
2
by: Duncan Smith | last post by:
Hello, In moving from 2.4 to 2.5 I find that some of my unit tests are now failing. I've worked out that the problem relates to the set update method. In 2.4 I could update a set with an...
6
by: | last post by:
Hi, I'm steel trying to read and update my XML file with Visual Basic Express but i am unable to find the right way to read my xml file and update it if neccessary... Here is my problem :...
0
by: Falcula | last post by:
Hello, I have a treeview that i fill from a database, when i update nodename in database the treeview dont update. Its works when iam not useing enableviewstate="true" but then i loosing the...
0
by: Peter Parker | last post by:
I am using php5 to parse node content from a xml file. Could someone show me if it's possible to to print the content without the !] tags in the following codes ? Thank you in advance .......
3
by: SM | last post by:
I think that simpleXML is the best thing i've seen in PHP5. It's so simple (d'ho!) and easy to use when it comes to reading a simple XML file. The only thing i'm not sure how to achieve is...
3
by: Michel Esber | last post by:
Hi all, DB2 V8 LUW FP 15 There is a table T (ID varchar (24), ABC timestamp). ID is PK. Our application needs to frequently update T with a new value for ABC. update T set ABC=? where ID...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.