473,836 Members | 1,549 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 5250
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.co m 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.co m 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_conten ts() 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_conten ts($my_file);

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

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

?>
jh****@gmail.co m 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_conten ts() 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_conten ts($my_file);

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

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

?>
jh****@gmail.co m 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->getElementsByT agName('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.co m 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_conten ts() 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_conten ts($my_file);

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

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

?>
jh****@gmail.co m 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
1818
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> */ $dom = new domdocument; @$dom->loadHTMLFile('http://www.example.com'); $xpath = new domxpath($dom); $xNodes = $xpath->query("//div//p/*"); $sText = '';
3
3632
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 being stored correctly. Whenever I attempt to delete a row from my application I get the following error. SQL0911N The current transaction has been rolled back because of a deadlock or timeout. Reason code "68". SQLSTATE=40001
1
3283
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 exists just to sort of synchronize them by changing the xml nodes attribute(s). somehow i always catch an exception "blabla has an invalid token" but i cannot find a solution for this. maybe someone of you people can tell me how to do this...
2
1370
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 iterable type derived from dict as the argument. I now find that the set is updated with the hash values of the items in my iterable, rather than the items themselves. Converting to a list first gets round the problem. My iterable type has the same...
6
7278
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 : evry day, i store the number of children in my classroom in my XML file. For exemple, on monday, my app ask me something like this : msgbox ("Are the 28 children here today ?",vbyesno)
0
1906
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 current selection and stuff in the tree. I post my code here, any idea ?
0
1459
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 .... $colors = $doc->getElementsByTagName("color"); foreach($colors as $node) { // to print only 'red' without the !] tags as in <color>!]</color>
3
13002
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 finding if an XML node is empty or if the node exist or doesn't exist. I'm searching the web, but got nothing yet. I was thinking of posting it here.
3
3978
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
9825
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9671
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10854
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10558
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10600
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10257
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
4459
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
4022
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3116
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.