Writing DOMDocument to XML file

In my lovely PHP application, I’ve loaded in an XML document as a DOM object, then edited it. However, I’m having trouble writing it to my XML file.
My code goes something like this:


$xmlDoc = new DOMDocument();
$xmlDoc->load("data.xml");
//various editations occur here, all the while keeping $xmlDoc as a DOMDocument
$document = $xmlDoc->save("data.xml");
$fp = fopen("data.xml", "w+");
fwrite($fp, $document) or die("Error writing to file");
fclose($fp);

I’m pretty sure that I shouldn’t be using fopen, fwrite, and fclose for a DOMDocument, but it was kind of a last-ditch attempt after I couldn’t make save() work.

Other items of note:
-No errors are returned, it seems to think it is functioning correctly.
-Both the .php and .xml files have all permissions enabled.
-Yes, everything is in the same directory right now.

I’m a bit of a PHP novice, so thanks in advance for the help!