PDA

View Full Version : Appending New XML To An XML File With PHP



Streakfury
27 Mar 2006, 02:21 PM
Evening all,

I know it might seem a trivial thing to ask at first, but how can I append some XML data to an XML file in PHP? It's not as simple as just using:

fopen ("filename", "a");

because I dont want to append the new XML right at the end of the file, rather, I want to insert it just before the closing root node.

For example, here's a simple XML file:



<test>
<chunk>
<name>geoff</name>
</chunk>
</test>


Now say I wanted to to insert this:



<chunk>
<name>greg</name>
</chunk>


The result I'm looking for would be this:



<test>
<chunk>
<name>geoff</name>
</chunk>
<chunk>
<name>greg</name>
</chunk>
</test>


How can I do this? I've tried using the PHP 'substr_replace()' and 'fopen()' functions, but to no avail. Maybe I'm going about it the right way but my syntax is buggered.

Any ideas?

Cheers all,

- Streak