Updating XML with php ?

AFB

Lifer
Jan 10, 2004
10,718
3
0
I am parsing an XML document.

ie :
<library>
<book>
<name>123123863</name>
<title>Random Book title</title>
<description>Sucks</description>
</book>
<library>

How can I add another book or change a feild in this one ?
 

oog

Golden Member
Feb 14, 2002
1,721
0
0
write the whole file back out again. for the most part you can't just insert/delete/replace something in the middle of a file. you can add to the end easily enough, but that's about it.
 

Czar

Lifer
Oct 9, 1999
28,510
0
0
phpbuilder.com

they have a xml parser somewhere that you can work with, basicly read the whole xml file into an array and then rewrite it to the xml file with the new info
 

AFB

Lifer
Jan 10, 2004
10,718
3
0
So I have to rewite the whole file ? Dayum, that sucks. Anyone else know of a better way ? I was just planning to do it like you where saying, but using objects for each item.
 

jonmullen

Platinum Member
Jun 17, 2002
2,517
0
0
There is no way to really get around haveing to rewrite the whole file. The way files work is you can either append like a log or buffer in and rewrite. Now its not like you are going to have to echo every line back to the file but if you need to add something to the middle the very basic process is read the file into memory print back all the file before where you are adding print back the new stuff, print back the rest of the file.