Modifying XML file

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
surath
Forum Newbie
Posts: 14
Joined: Fri Jun 15, 2007 8:59 am

Modifying XML file

Post by surath »

Hi all, I was wondering if its possible to add elements onto an existing XML file and how that would happen. To be more elaborate, I have a xml file like following structure:

<ASX version="3.0">
<ENTRY><REF href="http://www.mydomain.com/File1.wmv" /></ENTRY>
<ENTRY><REF href="http://www.mydomain.com/File2.wmv" /></ENTRY>
<ENTRY><REF href="http://www.mydomain.com/File3.wmv" /></ENTRY>
</ASX>

Now, I have a situation where I am required to insert another entry like "<ENTRY><REF href="http://www.mydomain.com/xxx.wmv" /></ENTRY>" in the above file structure. Obviously every time a new xml file write (for just a single new entry) will not be a good solution to my problem.

Anyone have any idea??? Thanks in advance..
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

"New xml file write?" Please explain.
surath
Forum Newbie
Posts: 14
Joined: Fri Jun 15, 2007 8:59 am

xml file edit

Post by surath »

Sorry for creating confusion.

Actually, all the solutions and suggestions I got to include a new entry in the existing xml file is to write the file again (I mean not just appending the new entry) with all the previous entries + the new entry. But that not seemed to be a feasible solution...because, say I already have 99 entries in that xml file. Now for the new 100-th entry I have to write the same file with all 99 + 1 new entry.


I think, now I've made it clear. Sorry my english is not good enough. Thank you.
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Re: xml file edit

Post by volka »

surath wrote:because, say I already have 99 entries in that xml file. Now for the new 100-th entry I have to write the same file with all 99 + 1 new entry.
And that's bad because .... ?
surath
Forum Newbie
Posts: 14
Joined: Fri Jun 15, 2007 8:59 am

Re: xml file edit

Post by surath »

volka wrote:
surath wrote:because, say I already have 99 entries in that xml file. Now for the new 100-th entry I have to write the same file with all 99 + 1 new entry.
And that's bad because .... ?
Isn't it a bad approach??? For just one new entry, I have to read the entire file for old entries, storing them, appending the new entry with those existing entries and then write the file again.

All I have to say is, Is there any way to edit the xml file and append the new entry into it?
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

You could open the file with read/write access, seek to the end of the file and search backwards until the filepointer is exactly before the </ASX> and then write the new line and </ASX>. But in general I wouldn't consider that better or more elegant.
User avatar
Gente
Forum Contributor
Posts: 252
Joined: Wed Jun 13, 2007 9:43 am
Location: Ukraine, Kharkov
Contact:

Post by Gente »

What for do you use your XML?
For example if it's created for outside access you can do following.
Write a little php script, which will output header, footer and include the middle part, stored in independent file.
This middle part you can append with new records ('a' or 'a+' flag of fopen() function).
Also don't forget to set correct Content-Type
Post Reply