Page 1 of 1

Modifying XML file

Posted: Fri Jun 15, 2007 9:09 am
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..

Posted: Fri Jun 15, 2007 9:16 am
by feyd
"New xml file write?" Please explain.

xml file edit

Posted: Fri Jun 15, 2007 9:53 am
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.

Re: xml file edit

Posted: Fri Jun 15, 2007 9:56 am
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 .... ?

Re: xml file edit

Posted: Fri Jun 15, 2007 10:04 am
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?

Posted: Fri Jun 15, 2007 10:09 am
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.

Posted: Fri Jun 15, 2007 10:11 am
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