Using PHP to update an XML document

Hello,

I realise there is another thread for this, but it is slightly different to what I have here I thought it would be best to start fresh.

I am attempting to create a xml document for a flash music player, which can be updated using a php form. I’ve been at it for hours and hours, and have got a half working piece of code, mashed together from many different sources and websites.

My problem is that when I try add a new “song” using the form, it completely overwrites the entire xml document.

This is what I have so far:
XML Document: I have manually placed some data in there for testing purposes.

<?xml version="1.0" encoding="utf-8"?>
<musiclist>
    <song>
        <artist>Gandhi Bo-Bandhi</artist>
        <title>Gandhi Dub</title>
        <url>songs/GandhiDub.mp3</url>
    </song>
    <song>
        <artist>L-Wiz</artist>
        <title>Girl From Codeine City</title>
        <url>songs/02 Girl From Codeine City.mp3</url>
    </song>
</musiclist>

Form & current xml contents: The data displays from the xml document correctly.

    
<h1>Playlist</h1>
<form action="playlistaction.php" method="post">
<fieldset>
<label for="artist">Artist:</label><input type="text" id="artist" name="artist" /><br />
<label for="title">Title:</label> <input type="text" id="title" name="title"/><br />
<label for="path">URL:</label> <input type="text" id="url" name="url" /> <br />
<input type="submit" />
</fieldset>
</form>
<h2>Current entries:</h2>
<p>Artist - Title - URL</p>
<?php
$doc = new DOMDocument();
$doc->load( 'musiclist.xml' );
  
$musiclist = $doc->getElementsByTagName( "song" );
foreach( $musiclist as $song )
{
  $artists = $song->getElementsByTagName( "artist" );
  $artist = $artists->item(0)->nodeValue;
  
  $titles= $song->getElementsByTagName( "title" );
  $title= $titles->item(0)->nodeValue;
  
  $urls = $song->getElementsByTagName( "url" );
  $url = $urls->item(0)->nodeValue;
  
  echo "<b>$artist - $title - $url
</b><br>";
  }
?> 

The form action: - This is the root of the problem & I would like to know if I’ve approached it completely wrong, and if so, what needs to be done to fix it.


<?php

  $musiclist = array();
  $musiclist [] = array(
                  "artist" => $_POST['artist'],
                "title" => $_POST['title'],
                "url" => $_POST['url']
  );
  
$doc = new DOMDocument();
$doc->formatOutput = true;
$r = $doc->createElement( "musiclist" );
$doc->appendChild( $r );

foreach( $musiclist as $song )
{
$b = $doc->createElement("song");

$title = $doc->createElement("title");
$title->appendChild(
$doc->createTextNode( $song["title"] )
);
$b->appendChild( $title );

$artist = $doc->createElement("artist");
$artist->appendChild(
$doc->createTextNode( $song["artist"] )
);
$b->appendChild( $artist );
$url = $doc->createElement("url");
$url->appendChild(
$doc->createTextNode( $song["url"] )
);

$b->appendChild( $url );
$r->appendChild( $b );
}
echo $doc->saveXML();
$doc->save("musiclist.xml")
?> 

So, instead of adding a new song at the end of the xml, the xml is completely wiped and replaced with the new data… Any ideas as to how I can fix this will be greatly apprecaited.